• mysql排序的问题与获取第几高的分数的信息


    1:先截图看效果

     

    2:完整的SQl语句

     1 SELECT * FROM studentscore;
     2 -- ------------------
     3 SET @maxscore=(SELECT MAX(score) FROM studentscore LIMIT 1);
     4   SELECT  MAX(A.score)score FROM studentscore A
     5 INNER join studentscore B  ON A.id=B.id
     6  AND A.score!=@maxscore LIMIT 1;
     7  
     8 -- ----------------
     9  SELECT 
    10  S.id,S.score, 
    11 @myrow:=@myrow+1  AS myrow
    12 FROM 
    13 (SELECT id,score
    14 FROM studentscore
    15 ORDER BY score DESC) S,(SELECT @myrow:=0)r ;
    16 
    17 -- ------------------
    18  SELECT 
    19  S.id,S.score, 
    20  (CASE WHEN 
    21  @sameScore=S.score THEN @myrow WHEN @sameScore:=S.score THEN @myrow:=@myrow+1 END) AS myrow
    22 FROM 
    23 (SELECT id,score
    24 FROM studentscore
    25 ORDER BY score DESC) S,(
    26 SELECT @myrow:=0,@sameScore:= NULL)r ;
    27 -- --------可以求N条----------
    28 SELECT * FROM(
    29 SELECT 
    30  S.id,S.score, 
    31  (CASE WHEN 
    32  @sameScore=S.score THEN @myrow WHEN @sameScore:=S.score THEN @myrow:=@myrow+1 END) AS myrow
    33 FROM 
    34 (SELECT id,score
    35 FROM studentscore
    36 ORDER BY score DESC) S,(
    37 SELECT @myrow:=0,@sameScore:= NULL)r
    38 ) qq
    39 WHERE myrow=2 -- 可以取 @N高的信息,具体看需求
    40 
    41 
    42 
    43 
    44 
    45 
    46 
    47 
    48 
    49 
    50   
    View Code
    如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!

    我的博客园地址:https://www.cnblogs.com/Fengge518

  • 相关阅读:
    数据类型
    注释
    编译型语言和解释型语言
    POJ1026 Cipher(置换的幂运算)
    最短路(代码来源于kuangbin和百度)
    POJ1753 Flip Game(bfs、枚举)
    POJ1860 Currency Exchange(bellman-ford)
    【转】博弈—SG函数
    【转】欧几里得与扩展欧几里得
    HDU 5833 Zhu and 772002(高斯消元)
  • 原文地址:https://www.cnblogs.com/Fengge518/p/13564742.html
Copyright © 2020-2023  润新知