如果用户具有相同的排名,则排名相同,然后跳过其他排名。
例如:下表显示用户5首先响应3次,因此他的排名将高于用户4然后用户1和其他人?我非常感谢任何帮助。谢谢。
SELECT name,
(select count(*)
from users u2
where u2.points > u.points or
u2.points = u.points and u2.sno <= u.sno
) as rank
FROM `users` u GROUP BY `name` , `sno`
ORDER BY CAST(points AS UNSIGNED) DESC;
USERS:
SNO points first_to_respond name
1 100 1 a
2 100 0 b
3 100 0 c
4 100 2 d
5 100 3 e
答案 0 :(得分:1)
按要求获得正确的排名:
SELECT name,
(select count(*) + 1
from users u2
where u2.points > u.points
) as rank
FROM `users` u;
这会计算包含更多点数的数字,然后将该值加1。
如果您想按其他因素排序,请执行以下操作:
SELECT name,
(select count(*) + 1
from users u2
where u2.points > u.points or
u2.points = u.points and u2.first_to_respond < u.first_to_respond
) as rank
FROM `users` u;
(我不确定第二个条件应该是<
还是>
。)