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;
上一篇:如何在Linux上配置基于Web的网络流量监控系统


下一篇:js 判断是数组还是对象还是字符串,判断字符串是否为空