Mysql在时间戳上使用Timedifference加入

时间:2013-12-10 10:17:45

标签: mysql join unique closest

我有两个表,killlog和deathlog记录死亡并从在线游戏中杀死信息。我想加入这两个表来制作一个杀戮板。

这可以通过查看时间戳记中的死亡和死亡记录,并加入每次杀戮的时间最接近的死亡。杀戮和死亡之间的时间差异通常相对较近,但可以达到10分钟。

我希望在时间差异方面将每次杀戮与CLOSEST死亡相匹配,但我只想将每次死亡与一次杀戮相匹配。

目前我有以下SQL语句

SELECT
distinct killlog.ID, deathlog.id, killlog.PlayerName, deathlog.PlayerName, killlog.Timestamp as killtime, deathlog.Timestamp as deathtime, 
TIMEDIFF(killlog.Timestamp, deathlog.Timestamp)
FROM killlog
inner JOIN deathlog on
TIMEDIFF(killlog.Timestamp, deathlog.Timestamp) <= 600 
GROUP by 
killlog.id

这几乎给了我想要的输出,但在某些情况下,我看到死亡与多次杀戮相匹配。例如:

+--------+---------+------------+------------+---------------------+---------------------+----------+
| killID | DeathID | KillerName | VictimName | killTime            | deathTime           | timeDiff |
+--------+---------+------------+------------+---------------------+---------------------+----------+
|      8 |      28 | DKmintz    | Hanfkeks   | 09/12/2013 21:30:23 | 09/12/2013 21:30:09 | 00:00:14 |
+--------+---------+------------+------------+---------------------+---------------------+----------+
|      9 |      28 | Sam Fisher | Hanfkeks   | 09/12/2013 21:31:03 | 09/12/2013 21:30:09 | 00:00:54 |
+--------+---------+------------+------------+---------------------+---------------------+----------+

在这里你可以看到同样的死亡与两种不同的杀戮相匹配。

我的问题是我如何确保我只返回一次死亡,并且没有将同样的死亡加入多次杀戮。

1 个答案:

答案 0 :(得分:0)

TIMEDIFF(killlog.Timestamp, deathlog.Timestamp) <= 600 
GROUP by

Here insted of groub by use partiotion by(kill time)<=600 and you will 
8   28  DKmintz Hanfkeks    09/12/2013 21:30:23 09/12/2013 21:30:09 00:00:14 record