我有MySql表,看起来像这样
quiz_id | user_id | points
--------+---------+--------
1 | 1 | 2
1 | 1 | 7
3 | 1 | 7
2 | 1 | 5
1 | 2 | 7
2 | 2 | 7
3 | 2 | 2
3 | 2 | 6
用户可以重播游戏,试图获得最高分。
我需要查询以获得用户玩的每个测验的最佳“MAX”分数,并按所有quizes的总和进行排序,并使其看起来像这样:
user_id | SUM(points)
--------+--------
2 | 20
1 | 19
我试图用这个
做第二部分SELECT user_id, SUM(points)
FROM Quiz_list
GROUP BY user_id
ORDER BY SUM(points) DESC
如果有人能告诉我它应该如何看作sql查询,我真的很感激。
答案 0 :(得分:3)
我认为你需要一个子查询来获取max()
,然后执行sum()
:
select user_id, sum(points) as sumpoints
from (select user_id, quiz_id, max(points) as points
from quiz_list
group by user_id, quiz_id
) uq
group by user_id
order by sumpoints desc;