我正在尝试在 Mysql 中执行查询以获取用户的所有时间排名。对于当月的排名,我设法通过以下要求做到了:
SELECT MAX(x.rank) AS rank
FROM (SELECT
t.id_user,
@rownum := @rownum + 1 AS rank
FROM screengame_points t
JOIN (SELECT @rownum := 0) r
WHERE month = $month
ORDER BY t.points DESC) x
WHERE x.id_user = 4
一直以来,我都需要计算所有点的总和,所以我尝试了:
SELECT MAX(x.rank) AS rank
FROM (SELECT
t.id_user,
@rownum := @rownum + 1 AS rank
FROM screengame_points t
JOIN (SELECT @rownum := 0) r
GROUP BY id_user
ORDER BY sum(t.points) DESC) x
WHERE x.id_user = 4
(注意“id_user 分组”和“sum(t.points)”)。
但它不起作用。我认为第二个请求中的排名结果是从一个月内得分最高的一个到最差的一个。它不是整个月所有点的总和。
我怎么能做到?