我需要在MySQL中对用户进行排名,其中排名考虑了两个关系,并继续将绑定用户计为排名的一部分..例如..
points rank
100 1
100 1
100 1
70 4
70 4
60 5
50 6
40 7
40 7
10 8
0 9
0 9
使用下面的代码,我的排名如下......
points rank game
100 1 1
100 1 1
100 1 1
70 2 1
70 2 1
60 3 1
50 4 1
40 5 1
40 5 1
10 6 1
0 7 1
0 7 1
UPDATE rank_table
JOIN (SELECT f.points ,
IF (@lastPoint <> f.points,
@curRank := @curRank +1,
@curRank) AS rank,
@lastPoint := f.points
FROM rank_table f
JOIN (SELECT @curRank := 0, @lastPoint := -1) r
WHERE f.game =1
ORDER BY f.points DESC
) ranks ON (ranks.points = rank_table.points)
SET rank_table.rank = ranks.rank WHERE rank_table.game =1;
有人知道这是可能的吗??
答案 0 :(得分:2)
您不需要任何mysql变量。
您的新排名是拥有比您更多积分的玩家数量。
update
result
join (
select
n.id, count(distinct q.id) total
from result n
left join result q
on
q.points > n.points
group by n.id) m
on
m.id = id
set rank=m.total + 1
(假设某种id
类似player_id
)