我有一个如下所示的 mysql 表:
I have a mysql table like below:
id name points
1 john 4635
3 tom 7364
4 bob 234
6 harry 9857
我基本上想在不选择所有用户的情况下获得单个用户排名.我只想通过 id 选择单个用户并获得由他们拥有的点数决定的用户排名.
I basically want to get an individual user rank without selecting all of the users. I only want to select a single user by id and get the users rank which is determined by the number of points they have.
例如,通过 id 3 选择排名 2 的 tom.
For example, get back tom with the rank 2 selecting by the id 3.
干杯
伊夫
SELECT uo.*,
(
SELECT COUNT(*)
FROM users ui
WHERE (ui.points, ui.id) >= (uo.points, uo.id)
) AS rank
FROM users uo
WHERE id = @id
密集排名:
SELECT uo.*,
(
SELECT COUNT(DISTINCT ui.points)
FROM users ui
WHERE ui.points >= uo.points
) AS rank
FROM users uo
WHERE id = @id
这篇关于MySQL,获取用户排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!