mysql 成绩排序

编写一个 SQL 查询来实现分数排名。

如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rank-scores
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

# Write your MySQL query statement below
SELECT t3.score 'Score', CONVERT(t4.rownum, SIGNED) 'Rank' FROM `Scores` t3
JOIN (SELECT score, @rownum:=@rownum+1 AS rownum
FROM (SELECT score FROM (SELECT * FROM `Scores` GROUP BY score) t ORDER BY t.score DESC) t1, (SELECT @rownum:=0) t2) t4
ON t3.score = t4.score ORDER BY t3.score DESC
posted @ 2020-04-24 17:21  梦见舟  阅读(500)  评论(0编辑  收藏  举报