我有一个包含500行数据的游戏排行榜,我编写了一个脚本以返回该数据,并且没有重复的分数。但是,我得到了重复的分数。这是我的脚本。
SELECT DISTINCT
username, score,
FIND_IN_SET(score, (SELECT DISTINCT GROUP_CONCAT(score ORDER BY score DESC)
FROM TPS_STATS)) AS rank
FROM
TPS_STATS
ORDER BY
rank ASC
LIMIT 100;
我看到的重复结果示例以图片形式发布。
答案 0 :(得分:1)
如果您的MySql版本是8.0,则可以使用row_number():
SELECT
username,
score,
row_number() OVER (ORDER BY score desc, username) rn
FROM TPS_STATS
ORDER BY score desc, username
LIMIT 100
请参见demo。
如果较低:
select
username,
score,
(select count(*) from TPS_STATS where score > t.score) +
(select count(*) from TPS_STATS where score = t.score and username < t.username) + 1
rank
from TPS_STATS t
order by rank, username
limit 100
请参见demo