下面的查询凉对于普通用户按分数排名次,但我需要在表中同分同等级结果的查询MySQL的通过成绩排名的用户/分
SET @rank = 0;
SELECT * FROM (
SELECT @rank:[email protected] + 1 AS rank,score, user_id FROM site_crossword_members ORDER BY score DESC
) as tmp;
感谢
下面的查询凉对于普通用户按分数排名次,但我需要在表中同分同等级结果的查询MySQL的通过成绩排名的用户/分
SET @rank = 0;
SELECT * FROM (
SELECT @rank:[email protected] + 1 AS rank,score, user_id FROM site_crossword_members ORDER BY score DESC
) as tmp;
感谢
你确定你的给定查询甚至可以正常工作我记得ORDER BY
要在SELECT
之后完成,因此使您的排名按ID ASC
排序或类似。
SELECT user_id,
CASE WHEN @score != score THEN @rank := @rank + 1 ELSE @rank END AS rank,
@score := score AS dummy_value
FROM ( SELECT score, user_id
FROM site_crossword_members,
(SELECT @rank := 0, @score := NULL) AS vars
ORDER BY score DESC) AS h
缩短版本:
SELECT user_id,
@rank := @rank + (@score != score) AS rank,
@score := score AS dummy_value
FROM ( SELECT score, user_id
FROM site_crossword_members,
(SELECT @rank := 0, @score := NULL) AS vars
ORDER BY score DESC) AS h
尝试此查询(用变量版本) -
SET @rank = 0;
SET @score = NULL;
SELECT
rank, score, user_id
FROM (
SELECT
score,
user_id,
IF(@score = score, @rank := @rank + 1, @rank := 1) AS rank,
@score := score
FROM site_crossword_members
ORDER BY score DESC
) t;
这个答案是正确的 –