0

我有一个像这个 id,username,points 的表 T,我想做的是为每个用户选择他的用户名和他的点的总和,并按点对行进行排序。我试过了,但没有用:

SELECT username, sum(points) as s
FROM T
    group by username,points
    order by points desc

结果:

username  points
3         2
3         3
3         0
4         0
6         0
7         0
3         -1
4

2 回答 2

3

如果您要汇总点数,则不想在 GROUP BY 语句中包含点数。此外,如果您将点的总和重命名为 s,您可以在 ORDER BY 中引用 s

SELECT username, sum(points) as s
FROM T
group by username
order by s desc
于 2013-03-12T17:29:30.847 回答
0

删除点的原因是因为如果您按点分组,则每个单独的值都分组:

SELECT username, sum(points) as s
FROM T
group by username
order by sum(points) desc

然后按 Sum(points) 排序

于 2013-03-12T17:33:46.023 回答