我有下表:
userid compid round score bonus
2980 3 0 50 0
50 3 0 80 0
52 3 0 80 0
55 3 0 20 0
58 3 0 100 0
106 3 0 120 0
555 3 0 50 0
100 3 0 30 0
50 3 1 90 0
52 3 1 50 0
106 3 1 30 0
我希望不仅可以对所有轮次的每个用户的得分和奖励总数求和,而且还可以相应地对它们进行排名。它应该能够处理序列1,2,2,4,5,5,5,8:
Trick是我不想创建临时表或将排名插入表中。此外,我无法限制回报 - 因此无论轮次完成,都需要对整个表进行排名。
我已设法对行进行排名,但似乎无法用总和对它们进行分组。当然,我已经单独管理了这笔钱 - 我只需要'加入'这两个步骤。
获得每位用户的总和:
SELECT userid,sum(score+bonus) as tscore from entries
group by userid order by tscore desc
userid tscore
50 170
106 150
52 130
58 100
2980 50
555 50
100 30
55 20
没有得分总和的排名:(感谢MySQL Cookbook)
SELECT @rownum := @rownum + 1 AS row,
@rank := IF(@prev_val!=score,@rownum,@rank) AS rank,
userid,
@prev_val := score AS score
FROM exodus_entries ORDER BY score DESC
row rank userid score
1 1 106 120
2 2 58 100
3 3 50 90
4 4 50 80
5 4 52 80
6 6 52 50
7 6 555 50
8 6 2980 50
9 9 100 30
10 9 106 30
11 11 55 20
感谢您的时间和帮助!欢迎所有建议!
答案 0 :(得分:3)
SELECT @rownum := @rownum + 1 AS row,
@rank := IF(@prev_val!=score,@rownum,@rank) AS rank,
userid,
@prev_val := score AS score
FROM (
SELECT userid, sum(score+bonus) as score
from entries
group by userid
) exodus_entries
ORDER BY score DESC
答案 1 :(得分:1)
我有同样的问题,我执行了
SET @rownum = 0, @rank = 1, @prev_val = NULL;
在运行以下代码之前:
SELECT @rownum := @rownum + 1 AS row,
@rank := IF(@prev_val!=total_number,@rownum,@rank) AS rank,
your_column,
@prev_val := total_number AS total_number
FROM your_table ORDER BY total_number DESC;
答案 2 :(得分:0)
我认为这应该可以解决问题:
SELECT userid,round, sum(score+bonus) as tscore from entries
group by userid, round order by tscore desc
虽未经过测试。