

法一:对于每个分数,找出大于或等于该分数的不重复分数,并按降序排列。
select score,
(select count(distinct score) from scores s2 where s2.score >= s1.score) rank
from scores s1
order by score desc
法二:利用join,条件是统计左表的分数小于等于右表分数的个数,并对右表进行计数。然后根据id分组,最后根据分数降序排列。
select s1.score,count(distinct s2.score) Rank
from scores s1 join scores s2 on s1.score <= s2.score
group by s1.id
order by s1.score desc
网友评论