我知道这应该很容易,但今天不是我的日子。
我有一张这样的表
user | points
---------|------------
Smith | 100
Neo | 200
Morpheus | 300
Smith | 100
Neo | 200
Morpheus | 300
而我正在寻找的选择会产生这个
user | points
---------|------------
Morpheus | 600
Neo | 400
所以,我想打印出总分超过300分的用户,我想按照从最高到最低的总和打印出来。
我正在使用:
$ mysql -V
mysql Ver 14.14 Distrib 5.1.61, for redhat-linux-gnu (x86_64) using readline 5.1
我一直在尝试的SQL是:
SELECT user, SUM(points) AS sum FROM users GROUP BY user HAVING SUM(points)>300
这给了我所有正确的输出,虽然它没有给我排序输出。我确实尝试插入ORDER BY但没有运气。
答案 0 :(得分:4)
SELECT user, SUM(points) AS sum
FROM users
GROUP BY user
HAVING sum > 300
order by sum desc
答案 1 :(得分:0)
应该只是
SELECT user, SUM(points) AS sum FROM users GROUP BY user HAVING SUM(points)>300
ORDER BY SUM(points) DESC
除非我遗漏了某些东西