2

我知道这应该很容易,但今天不是我的日子。

我有一张这样的桌子

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 但没有运气。

4

2 回答 2

4
SELECT user, SUM(points) AS sum 
FROM users 
GROUP BY user 
HAVING sum > 300
order by sum desc
于 2012-05-03T12:03:07.057 回答
0

应该只是

SELECT user, SUM(points) AS sum FROM users GROUP BY user HAVING SUM(points)>300 
ORDER BY SUM(points) DESC

除非我错过了什么

于 2012-05-03T12:05:03.970 回答