按每个玩家的时间长度排序最大分数

时间:2012-08-16 10:13:51

标签: mysql sql

我的数据库结构

Table 'Player'   : column 'id'
Table 'Score'    : column 'player_id' , 'score' , 'time'
表'分数'中的

示例数据

   player_id       score             time
     1001           100      2000-01-01 00:00:00
     1001             3      2012-08-01 00:00:00
     1001            11      2012-08-02 00:00:00
     1002            80      2012-07-01 00:00:00
     1002             5      2012-08-01 00:00:00
     1003            90      2012-07-01 00:00:00

我尝试使用

SELECT * FROM Score WHERE MONTH(time) = MONTH(NOW()) ORDER BY score DESC

来自玩家1001的结果将是得分11和3的多行 如何排序并获得时间长度的最大分数,该分数应仅为玩家1001的得分11

结果应为

   player_id       score             time
     1001            11      2012-08-02 00:00:00
     1002             5      2012-08-01 00:00:00

   player_id       score             time
     1001            11      2012-08-02 00:00:00
     1001             3      2012-08-01 00:00:00
     1002             5      2012-08-01 00:00:00

2 个答案:

答案 0 :(得分:2)

SELECT player_id, max(score) as max_score
FROM Score 
WHERE MONTH(time) = MONTH(NOW()) 
group by player_id
ORDER BY max_score DESC

答案 1 :(得分:1)

试试这个:

SELECT player_id, max(score) as score, month(time)
FROM Score 
WHERE MONTH(time) = MONTH(NOW()) 
GROUP BY Player_id, month(time)
ORDER BY score DESC