SQL 查询未返回不同的值
SQL query not returning distinct values
我有一个包含 500 行数据的游戏排行榜,我为 return 数据编写了一个脚本,没有重复的分数。但是,我收到了重复的分数 returned。这是我的脚本。
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;
我看到的重复结果示例已作为图片发布。
如果您的 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
我有一个包含 500 行数据的游戏排行榜,我为 return 数据编写了一个脚本,没有重复的分数。但是,我收到了重复的分数 returned。这是我的脚本。
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;
我看到的重复结果示例已作为图片发布。
如果您的 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