zoukankan      html  css  js  c++  java
  • MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中

    一、排名

    /*普通排名:从1开始,顺序往下排*/
    SELECT cs.*,@r :=@r + 1 AS rank
    FROM cs,(SELECT @r := 0) r
    ORDER BY score;

    /*并列排名:相同的值是相同的排名*/
    SELECT cs.* ,
    CASE 
    WHEN @p=score THEN @r
    WHEN @p:=score THEN @r:=@r+1
    END rank
    FROM cs,(SELECT @r:=0,@p:=NULL)r
    ORDER BY score;

     

    复制代码
    /*并列排名:相同的值名次相同,与上例中的并列排名不同*/
    SELECT city,score,rank
    FROM
    (
    SELECT cs.*,
    @c:=IF(@p=score,@c,@r) AS rank,
    @p:=score,
    @r:=@r+1
    FROM cs ,(SELECT @p:=NULL,@r:=1,@c:=0)r
    ORDER BY score
    )c
    复制代码

     

     二、分组后组内排名

    复制代码
    /*分组普通排名:顺序排名*/
    SELECT city,score,rank
    FROM
    (
    SELECT cs.*,IF(@p=city,@r:=@r+1,@r:=1) AS rank,
        @p:=city
    FROM cs,(SELECT @p:=NULL,@r:=0)r
    ORDER BY city,score
    )s;
    复制代码

     

    复制代码
    /* 分组后并列排名:组内相同数值排名相同*/
    SELECT city,score,rank
    FROM
    (
    SELECT *,
    IF(@p=city,
        CASE 
           WHEN @s=score THEN @r
           WHEN @s:=score THEN @r:=@r+1
        END,
       @r:=1 ) AS rank,
    @p:=city,
    @s:=score
    FROM cs,(SELECT @p:=NULL,@s:=NULL,@r:=0)r
    ORDER BY city,score 
    )s;
    复制代码

     三、分组后取各组的前两名

    复制代码
    /*取每组分数高的前两个,法一*/
    SELECT city,score,rank
    FROM
    (
    SELECT *,
    IF(@p=city,
        CASE 
            WHEN @s=score THEN @r
            WHEN @s:=score THEN @r:=@r+1
        END,
      @r:=1 ) AS rank,
    @p:=city,
    @s:=score
    FROM cs,(SELECT @p:=NULL,@s:=NULL,@r:=0)r
    ORDER BY city,score DESC 
    )s
    WHERE rank <3;
    复制代码

    /*分组后取前两个,法二*/
    SELECT * FROM cs c
    WHERE (
        SELECT count(*) FROM cs
        WHERE city=c.city AND score>c.score )<2
      ORDER BY city,score DESC

    如何用mysql 查询出数据出数据排名,然后加编号;

    表字段说明:查询e_Task_Result表 ,score代表分数,task_result_id 代表id

    SELECT
    (@rowNO := @rowNo+1) AS rowno,
    task_result_id ,score FROM (
    (SELECT task_result_id ,score FROM e_task_result ORDER BY score DESC) a,(SELECT @rowNo :=0) b )

    如何将查询出来的排名更新到表中
    表字段说明:查询e_Task_Result表 ,score代表分数,task_result_id 代表id,rank代表排名

    UPDATE e_task_result d
    LEFT JOIN (SELECT
    (@rowNO := @rowNo+1) AS rowno,
    task_result_id
    FROM (SELECT
    task_result_id
    FROM e_task_result etr
    ORDER BY task_result_status DESC,etr.score DESC,end_date) a,
    (SELECT
    @rowNO :=0) b) c
    ON c.task_Result_id = d.task_result_id
    SET d.rank = rowno

    再如:

    UPDATE 
    (SELECT @a := @a + 1 AS rank, user_code, task_type 
    FROM ( 
    select * from user_score_quality 
    where  task_type =1  
    ORDER BY user_score_quality.last_finish_task_num desc, user_score_quality.update_time asc
    ) h, (SELECT @a := 0 ) t 
    ) t1, user_score_quality t2 
    
    SET t2.last_rank = t1.rank 
    WHERE t2.user_code = t1.user_code  AND  t2.task_type=t1.task_type

    参考:

    https://blog.csdn.net/w329636271/article/details/51751282 

    https://www.cnblogs.com/niniya/p/9046449.html

    https://www.jianshu.com/p/bb1b72a1623e

    http://blog.sina.com.cn/s/blog_4c197d420101e408.html

  • 相关阅读:
    HTML链接/实施CSS的三种方法
    XML之Well-Formed文档规则
    【摘】SVN提交与版本冲突
    Web开发之404小结
    TCP 连接的要点
    [转] Epoll 相对Poll和Select的优点
    [转] 剖析 epoll ET/LT 触发方式的性能差异误解(定性分析)
    GDB调试技巧
    [转] 关于c++的头文件依赖
    [转] Linux中gcc,g++常用编译选项
  • 原文地址:https://www.cnblogs.com/yuluoxingkong/p/10606425.html
Copyright © 2011-2022 走看看