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;

    __________________________________________________

  • 相关阅读:
    javascript深入理解js闭包
    hibernate 之 sql查询
    MongoDB 2.4企业版分析
    MongoDB 连接池
    GridFS实现原理
    MongoVUE破解
    mongodb 官方 手册
    mongodb的一些性能管理工具
    Python: names, values, assignment and mutability
    使用 mock 测试
  • 原文地址:https://www.cnblogs.com/bravesunforever/p/13190320.html
Copyright © 2011-2022 走看看