如何在mysql中每天找到最好的播放器

时间:2015-01-29 00:33:33

标签: mysql

我有一个像这样的mysql表:

+----+--------+-------------+-------+--------------+---------------------+
| id | name   | country     | score | email        | timestamp           |
+----+--------+-------------+-------+--------------+---------------------+
|  2 | John   | Canada      |   360 | john@abc.com | 2015-01-28 14:23:35 |
|  3 | Peter  | USA         |   830 | NULL         | 2015-01-28 16:23:35 |
|  4 | Victor | New Zealand |   780 | NULL         | 2015-01-27 06:33:55 |
|  5 | Sandy  | UK          |   880 | NULL         | 2015-01-29 02:09:58 |
|  6 | Craig  | New Zealand |   980 | NULL         | 2015-01-29 02:11:26 |
|  7 | Susan  | New Zealand |   580 | NULL         | 2015-01-29 02:11:57 |
+----+--------+-------------+-------+--------------+---------------------+

表名'得分'我想知道谁是每天最好的球员?我是mysql的新手。因此,如果有人能够帮助我找出每天在mysql中最佳玩家的方式,将会非常感激!

结果,我想要实现的只是每天最好的球员:

+----+--------+-------------+-------+--------------+---------------------+
| id | name   | country     | score | email        | timestamp           |
+----+--------+-------------+-------+--------------+---------------------+
|  6 | Craig  | New Zealand |   980 | NULL         | 2015-01-29 02:11:26 |
|  3 | Peter  | USA         |   830 | NULL         | 2015-01-28 16:23:35 |
|  4 | Victor | New Zealand |   780 | NULL         | 2015-01-27 06:33:55 |
+----+--------+-------------+-------+--------------+---------------------+

提前谢谢!

1 个答案:

答案 0 :(得分:1)

您将使用聚合和连接:

select t.*
from scores t join
     (select date(timestamp) as dte, max(score) as maxscore
      from scores t
      group by date(timestamp)
     ) tt
     on tt.dte = date(t.timestamp) and tt.maxscore = t.score;

注意:如果您有重复项,则会返回所有具有相同最高分数的人。