mysql查询结果进行排名

 

1.查询表,获得结果,然后进行排名:

SELECT * FROM shopping_activity_Result_log ORDER BY totalRate DESC

结果是:

2.进行排序,注意同结果排名相同并且占用名次

SELECT
    * 
FROM
    (
    SELECT
        obj.user_id,
        obj.`day`,
        obj.totalRate,
        obj.exeTime,
    CASE
            
            WHEN @last_score = obj.totalRate THEN
            @rank 
            WHEN @last_score := obj.totalRate THEN
            @rank := @rank + 1 
            WHEN @last_score = 0 OR @last_score IS NULL THEN
          @rank := @rank + 1
        END AS ranking 
    FROM
        ( SELECT * FROM shopping_activity_Result_log ORDER BY totalRate DESC ) obj,
    ( SELECT @rank := 0, @last_score := NULL) r 
    ) a

 

  想要的效果就出啦了。

 

posted @ 2019-06-28 16:20  坠落凡尘的魔鬼  阅读(4028)  评论(1编辑  收藏  举报