题目链接:
解析:
刚开始看到这道题我的写法是这样的:
SELECT Score,
(SELECT COUNT(*) FROM Scores S1 WHERE S1.Id != S2.Id AND S1.Score >= S2.Score) AS Rank
FROM Scores S2
ORDER BY Rank;
但是这样的输出Rank结果为:1,1,2,4,4,5。不满足题目要求,所以这里我们应该使用DISTINCT
关键字来进行处理。
题解:
# Write your MySQL query statement below
SELECT Score,
(SELECT COUNT(DISTINCT Score) FROM Scores S1 WHERE S1.Score >= S2.Score) AS Rank
FROM Scores S2
ORDER BY Rank;
网友评论