我认为这是 best solution .然而,这个查询并没有解决我的问题——我喜欢这张表:
+--+-------+-----+
|id|user_id|score|
+--+-------+-----+
|1 |1 |5 |
+--+-------+-----+
|2 |1 |16 |
+--+-------+-----+
|3 |1 |15 |
+--+-------+-----+
查询:
SELECT *
FROM (`_scorboard`)
GROUP BY `user_id`
HAVING `score` = MAX(score)
ORDER BY `score` desc
result 0 rows
为什么返回0条记录?
最佳答案
使用:
SELECT a.*
FROM SCOREBOARD a
JOIN (SELECT t.user_id,
MAX(t.score) AS max_score
FROM SCOREBOARD t
GROUP BY t.user_id) b ON b.max_score = a.score
AND b.user_id = a.user_id
如果你想要那些在表格中得分最高的人:
SELECT a.*
FROM SCOREBOARD a
JOIN (SELECT MAX(t.score) AS max_score
FROM SCOREBOARD t) b ON b.max_score = a.score
关于mysql 在分组前排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5930965/