我有以下SQL脚本,它在排行榜表中对一组用户进行排名。
UPDATE leaderboard
JOIN (SELECT f.winnings ,
IF (@lastPoint <> f.winnings,
@curRank := @curRank +1,
@curRank) AS rank,
@lastPoint := f.winnings
FROM leaderboard f
JOIN (SELECT @curRank := 0, @lastPoint := 0) r
WHERE f.competition =5
ORDER BY f.winnings DESC
) ranks ON (ranks.winnings = leaderboard.winnings)
SET leaderboard.rank = ranks.rank WHERE leaderboard.id =89;
然而,当所有用户的等级为0时,它不会将它们排在1(即相等),这种行为只发生在0。
winnings rank
0 0
0 0
0 0
有谁知道怎么做...?
winnings rank
0 1
0 1
0 1
SQL FIDDLE http://sqlfiddle.com/#!2/53c3a/1
答案 0 :(得分:1)
只需将@LastPoint
设置为无效值:
UPDATE leaderboard
JOIN (SELECT f.winnings ,
IF (@lastPoint <> f.winnings,
@curRank := @curRank +1,
@curRank) AS rank,
@lastPoint := f.winnings
FROM leaderboard f
JOIN (SELECT @curRank := 0, @lastPoint := -1) r
ORDER BY f.winnings DESC
) ranks ON (ranks.winnings = leaderboard.winnings)
SET leaderboard.rank = ranks.rank;
答案 1 :(得分:1)
<>
测试失败
UPDATE leaderboard
JOIN (SELECT f.winnings ,
IF (@lastPoint <> f.winnings,
@curRank := @curRank +1,
@curRank) AS rank,
@lastPoint := f.winnings
FROM leaderboard f
JOIN (SELECT @curRank :=0, @lastPoint :=-1) r
ORDER BY f.winnings DESC
) ranks ON (ranks.winnings = leaderboard.winnings)
SET leaderboard.rank = ranks.rank;