1

出色地。我真的不知道我想要的东西是否可以通过我构建表格的方式实现。让我解释。

我有这张桌子

CREATE TABLE `kill_log` (
  `time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `killer_cid` int(11) NOT NULL,
  `killed_cid` int(11) NOT NULL,
  `map` varchar(25) NOT NULL DEFAULT '',
  KEY `killer_cid` (`killer_cid`),
  KEY `killed_cid` (`killed_cid`)
)

当有人在游戏中杀死某人时,它会被注册,并且在每个月底我需要通过计算被杀死的次数和被杀死的次数来计算获胜者。

我尝试用组建立一个查询,但我不知道如何计算他被杀了多少次..

SELECT killer_cid as char_id, count(killer_cid) as kills
FROM kill_log
WHERE `time` >= (NOW() - INTERVAL 30 DAY)
GROUP BY killer_cid
ORDER BY count(killer_cid) DESC

但我真正需要的是展示

char_id  |  kills  |  deaths  |  points (kills - deaths)

我怎么能这样做?我应该创建一个不同的表并以另一种方式注册东西吗?

我想我可以记录他们什么时候被杀和什么时候死,然后分组char_id并计算他们是否被杀或死了..但是..这种方法可行吗?

4

2 回答 2

2
SELECT U.char_ID, SUM(U.kills) as kills, SUM(U.killed) as deaths, SUM(U.kills)- SUM(U.killed) as points FROM
(
SELECT killer_cid as char_id, count(killer_cid) as kills, 0 as killed
FROM kill_log
WHERE `time` >= (NOW() - INTERVAL 30 DAY)
GROUP BY killer_cid
ORDER BY count(killer_cid) DESC

UNION

SELECT killed_cid as char_id,0 AS kills, count(killer_cid) as killed
FROM kill_log
WHERE `time` >= (NOW() - INTERVAL 30 DAY)
GROUP BY killed_cid
ORDER BY count(killer_cid) DESC
) AS U
GROUP BY U.char_ID
ORDER BY SUM(U.kills)- SUM(U.killed) DESC
于 2012-06-29T17:22:01.803 回答
0
select
 killer.killer_cid as cid,
 count(distinct killer.killed_cid, killer.time) as kills,
 count(distinct killed.killer_cid, killed.time) as died,
 count(distinct killer.killed_cid, killer.time) - count(distinct killed.killer_cid, killed.time) as points
from kill_log as killer
left join kill_log as killed (killer.killer_cid = killed.killed_cid)
group by killer.killer_cid
于 2012-06-29T17:15:24.177 回答