1

我的数据库结构

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
4

2 回答 2

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
于 2012-08-16T10:15:23.870 回答
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
于 2012-08-16T10:15:35.127 回答