zoukankan      html  css  js  c++  java
  • mysql计算排名【转】

    mysql计算排名,获取行号rowno

    学生成绩表数据

    SELECT * FROM table_score ORDER BY score DESC;
    

    获取某个学生成绩排名并计算该学生和上一名学生成绩差,是并列排名

    SELECT *,
    (SELECT count(DISTINCT score) FROM table_score AS b WHERE a.score<b.score)+1 AS rank, #获取排名,并列
    (SELECT b.score FROM table_score AS b WHERE b.score>a.score ORDER BY b.score LIMIT 1)-a.score AS subtract #获取和上一名学生成绩的差 
    FROM table_score AS a WHERE a.s_id = 13; #获取学生周一的成绩排名和与上一名的成绩差
    

    获取所有学生成绩排名-并列排名

    SELECT *,
    (SELECT count(DISTINCT score) FROM table_score AS b WHERE a.score<b.score)+1 AS rank #获取排名-并列
    FROM table_score AS a ORDER BY rank; #获取学生成绩排名
    

    获取所有学生成绩排名,不是并列排名。计算行号进行排名

    SELECT a.*,
    (@rowNum:=@rowNum+1) AS rank #计算行号 
    FROM table_score AS a,
    (SELECT (@rowNum :=0) ) b
    ORDER BY a.score DESC;
    

     
    来源:https://www.cnblogs.com/aeiou/p/5719396.html
    更多参考:https://www.cnblogs.com/-mrl/p/9073375.html

  • 相关阅读:
    单选多选样式写法
    深拷贝方法
    防抖和节流的实现
    yarn 常用指令
    前端性能监控
    全表 or 索引
    Order by
    DINSTINCT
    智力题
    概率问题
  • 原文地址:https://www.cnblogs.com/KillBugMe/p/13259838.html
Copyright © 2011-2022 走看看