天天看点

MYSQL实现排名函数RANK,DENSE_RANK和ROW_NUMBER

  1. 并列跳跃排名
select
       score,
       (
           select count(score) + 1 from score s2 
           where s2.score > s.score
       ) ranking
from score s order by score desc;           
  1. 并列连续排名
select
       score,
       (
           select count(distinct score) from score s2
           where s2.score >= s.score
       ) ranking
from score s order by score desc;           
  1. 分组并列跳跃
select
       score,
       course_id,
       (
           select count(score) + 1 from score s2
           where s2.course_id = s.course_id and s2.score > s.score
        ) ranking
from score s
order by course_id,score desc;           
  1. 分组并列连续
select
       score,
       course_id,
       (
           select count(distinct score) from score s2
           where s2.course_id = s.course_id and s2.score >= s.score
        ) ranking
from score s
order by course_id,score desc;           

继续阅读