在两个条件下模拟mysql中的等级(无等级) [英] Simulate rank in mysql (without rank) with two conditions

查看:103
本文介绍了在两个条件下模拟mysql中的等级(无等级)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自此答案:基于两列的排名

我有以下示例:

CREATE TABLE tmpPoradi 
    (`player` int, `wins` int, `diff` int)
;

INSERT INTO tmpPoradi 
    (`player`, `wins`, `diff`)
VALUES
    (1, 10, 12),
    (2, 8, 2),
    (3, 10, 10),
    (4, 8, 1),
    (5, 8, 7),
    (6, 10, 14),
    (8, 10, 10),
   (7, 12, 3)
;

,所需的结果必须是:

+--------+------+------+------+
| player | wins | diff | rank |
+--------+------+------+------+
|      7 |   12 |    3 |    1 |
|      6 |   10 |   14 |    2 |
|      1 |   10 |   12 |    3 |
|      3 |   10 |   10 |    4 |
|      8 |   10 |   10 |    4 |
|      5 |    8 |    7 |    5 |
|      2 |    8 |    2 |    6 |
|      4 |    8 |    1 |    7 |
+--------+------+------+------+

问题是原始答案返回行数:

The thing is that the original answer returns the row count:

 SELECT player, wins, diff,rank from
 (
 SELECT player, wins, diff, @winrank := @winrank + 1 AS rank
 from tmpPoradi,(SELECT @winrank := 0) r 
 ORDER BY wins DESC,diff DESC
 )  rt
ORDER BY rank 

返回:

+---------+-------+-------+-------+
|  player |  wins |  diff |  rank |
+---------+-------+-------+-------+
|       7 |    12 |     3 |     1 |
|       6 |    10 |    14 |     2 |
|       1 |    10 |    12 |     3 |
|       3 |    10 |    10 |     4 |
|       8 |    10 |    10 |     5 |
|       5 |     8 |     7 |     6 |
|       2 |     8 |     2 |     7 |
|       4 |     8 |     1 |     8 |
+---------+-------+-------+-------+

我的有条件尝试会返回以下结果:(不知道为什么)

and my attemp with conditions returns the following result: (dont know why)

  SELECT player, wins, diff,rank from
 (
 SELECT player, wins, diff, @winrank := IF(wins = diff,@winrank,@winrank + 1) AS rank
 from tmpPoradi,(SELECT @winrank := 0) r 
 ORDER BY wins DESC,diff DESC
 )  rt
ORDER BY rank 


+---------+-------+-------+-------+
|  player |  wins |  diff |  rank |
+---------+-------+-------+-------+
|       7 |    12 |     3 |     1 |
|       6 |    10 |    14 |     2 |
|       3 |    10 |    10 |     3 |
|       8 |    10 |    10 |     3 |
|       1 |    10 |    12 |     3 |
|       5 |     8 |     7 |     4 |
|       2 |     8 |     2 |     5 |
|       4 |     8 |     1 |     6 |
+---------+-------+-------+-------+

所以问题是:

我在做错什么,如何才能按获胜排名,如果获胜并列,则按diff排名,然后如果diff被并列,排名相同?

推荐答案

SQL DEMO

SQL DEMO

  SELECT player, wins, diff, dense_rank, rank, dense_val, prev_wins, prev_diff
  FROM
     (
         SELECT player, 
                wins,                 
                diff, 
                @dense_rank := IF(wins = @prev_wins and diff = @prev_diff, @dense_rank, @dense_rank + @dense_val ) AS dense_rank,                
                @dense_val  := IF(wins = @prev_wins and diff = @prev_diff, @dense_val + 1 , 1) as dense_val,
                @rank := @rank + 1 as rank,
                @prev_wins := wins as prev_wins,
                @prev_diff := diff as prev_diff

         FROM tmpPoradi,(SELECT @dense_rank := 0, @dense_val := 1, @rank := 0, @prev_wins := 0, @prev_diff := 0) r 
         ORDER BY wins DESC,diff DESC
     )  rt
  ORDER BY rank 

输出

这篇关于在两个条件下模拟mysql中的等级(无等级)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆