zoukankan      html  css  js  c++  java
  • MySQL排名 保留名次空缺 问题

    思路:主要是看每门课程下,对于每个分数,该课程大于该分数的有几个

    保留名次空缺:

    select a.cid, a.sid, a.score , count(a.score<b.score)+1 as rank
    from sc a left join sc b on a.cid=b.cid and a.score<b.score   #笛卡尔积连接, 然后筛选满足a.score<b.score的
    group by a.cid, a.sid
    order by a.cid, a.score desc;

    不保留名次空缺:

    select a.cid, a.sid, a.score , count(distinct b.score)+1 as rank  #这里使用的是distinct b.score  
    from sc a left join sc b on a.cid=b.cid and a.score<b.score   #笛卡尔积连接, 然后筛选满足a.score<b.score的
    group by a.cid, a.sid
    order by a.cid, a.score desc;

    注意:20211017更新,找到原因了,上次编辑时还不能使用rank over排名函数,所以select语句中as rank是没问题的,这次总是报错,是因为rank是个函数关键字,所以报错,命名成中文或其他的英文名就没问题了

    中间表的形式:

    select a.cid,a.sid,a.score,b.cid,b.sid,b.score
    from  sc a left join sc b on a.cid=b.cid and a.score< b.score
    order by a.cid, a.score desc;

    __________________________________________________

  • 相关阅读:
    卸载office密钥的命令
    断言的使用
    stm32的NVIC是什么?
    STM32 Cube mx 安装
    不用移位计算获得高位数据
    分组数据
    Vue Router
    存储过程
    js 中 json.stringfy()将对象、数组转换成字符串
    js中 json对象的转化 JSON.parse()
  • 原文地址:https://www.cnblogs.com/bravesunforever/p/13190320.html
Copyright © 2011-2022 走看看