mysql 在分组之前排序
我认为这是最佳解决方案。但是,这个查询并没有解决我的问题 - 我有这样的表:
+--+-------+-----+
|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 条记录?
I think this is the best solution. However, this query is not solve my issue - I have like this table:
+--+-------+-----+
|id|user_id|score|
+--+-------+-----+
|1 |1 |5 |
+--+-------+-----+
|2 |1 |16 |
+--+-------+-----+
|3 |1 |15 |
+--+-------+-----+
Query:
SELECT *
FROM (`_scorboard`)
GROUP BY `user_id`
HAVING `score` = MAX(score)
ORDER BY `score` desc
result 0 rows
Why is it returns 0 records ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
如果您想要表中得分最高的人:
Use:
If you want those who have the highest score in the table:
由于查询中有一个
GROUP BY
子句,MySQL 首先按1
的user_id
进行分组,选择它喜欢的任何行。然后,HAVING
子句将应用于这些选定的行。由于所选行可能是也可能不是MAX
值为score
的行,因此查询返回 0 个结果。正确的做法是:
Since you have a
GROUP BY
clause in your query, MySQL groups by theuser_id
of1
first, choosing any of the rows that it pleases. TheHAVING
clause then applies to these selected rows. Since the selected row may or may not be the one with theMAX
value ofscore
, the query is returning 0 results.The correct way to do is: