我在使用计数和比率订购MySQL查询的方式遇到了问题。
这是我目前正在使用的代码
$getKillers = mysql_query("SELECT `killer`, `killed`, count(`killer`), count(`killed`) FROM `kills` GROUP BY `killer` ORDER BY count(`killer`) / count(`killed`) DESC LIMIT {$limit}");
这是我正在使用的数据库结构
CREATE TABLE `kills` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`killer` varchar(20) NOT NULL COMMENT 'The player who scored the kill',
`killed` varchar(20) NOT NULL COMMENT 'The player who was killed',
PRIMARY KEY (`id`),
KEY `killer` (`killer`,`killed`)
) ENGINE=InnoDB AUTO_INCREMENT=680633 DEFAULT CHARSET=utf8 COMMENT='PVP Log';
-- ----------------------------
-- Records of kills
-- ----------------------------
INSERT INTO `kills` VALUES ('1', 'Player1', 'Player2');
INSERT INTO `kills` VALUES ('2', 'Player1', 'Player3');
INSERT INTO `kills` VALUES ('3', 'Player2', 'Player3');
INSERT INTO `kills` VALUES ('4', 'Player2', 'Player1');
INSERT INTO `kills` VALUES ('5', 'Player1', 'Player2');
INSERT INTO `kills` VALUES ('6', 'Player3', 'Player3');
INSERT INTO `kills` VALUES ('7', 'Player2', 'Player3');
INSERT INTO `kills` VALUES ('8', 'Player3', 'Player1');
我希望这个SQL在PHP中使用mysql_fetch_array来显示具有KDR(杀死率)的顶级PVP,这对杀死是杀手。这是他们杀死某人的次数。
谢谢你的帮助!
答案 0 :(得分:1)
尝试此查询:
SELECT * , (SELECT COUNT( 1 )
FROM kills b
WHERE nested.killer = b.killed
) AS killed_cnt
FROM (
SELECT `killer` , COUNT( `killer` ) AS kill_cnt
FROM `kills` a
GROUP BY `killer`
) nested
ORDER BY kill_cnt / killed_cnt DESC
LIMIT 10