Mysql排名条件

时间:2016-05-27 10:23:05

标签: php mysql

我有一个比赛结果表如下:

table result_race_to_registration 

 id  | event_id |  result_race_id | registration_id  |  subclass_id  | 
---------------------------------------------------------------------
1   |     1    |        21       |        11          |       1     |
---------------------------------------------------------------------
2   |     1    |        22       |        12          |       1     |
---------------------------------------------------------------------
3   |     1    |        23       |        13          |       1     |
---------------------------------------------------------------------
4   |     1    |        24       |        14          |       2     |
---------------------------------------------------------------------
5   |     1    |        25       |        15          |       2     |
---------------------------------------------------------------------
6   |     1    |        26       |        16          |       2     |
---------------------------------------------------------------------
7   |     2    |        27       |        17          |       1     |
---------------------------------------------------------------------
8   |     2    |        28       |        18          |       1     |
---------------------------------------------------------------------
9   |     2    |        29       |        19          |       1     |
---------------------------------------------------------------------
10  |    2     |        30       |        20          |       2     |
---------------------------------------------------------------------
11  |     2    |        31       |        21          |       2     |
---------------------------------------------------------------------
12  |     2    |        32       |        22          |       2     |
---------------------------------------------------------------------

Expected result 
rank  | event_id |  result_race_id | registration_id    | subclass_id | 
------------------------------------------------------------------------
   1   |    1     |        21       |        11          |       1     |
------------------------------------------------------------------------
   2   |    1     |        22       |        12          |       1     |
------------------------------------------------------------------------
   3   |    1     |        23       |        13          |       1     |
------------------------------------------------------------------------
   1   |    1     |        24       |        14          |       2     |
------------------------------------------------------------------------
   2   |    1     |        25       |        15          |       2     |
------------------------------------------------------------------------
   3   |    1     |        26       |        16          |       2     |
------------------------------------------------------------------------

如何通过event_id获取每个subclass_id过滤器的排名。  我已经尝试过这样的查询

SELECT @rank:=CASE WHEN @subclass = rtr.subclass_id  THEN @rank+1 ELSE 1 END AS rank, @subclass:= rtr.subclass_id AS subclass_id_1 , rtr.result_race_id, FROM result_race_to_registration rtr WHERE rtr.event_id = '$event_id' ORDER BY rtr.subclass_id,rank ASC  

但每当我将event_id放在WHERE条件上时,每个子类的等级都无效。请给出一些线索。感谢

1 个答案:

答案 0 :(得分:1)

尝试使用此功能,2个子查询应该有帮助

SET @event_ID=0, @subclass_ID=0, @Rank = 0;
SELECT 
    b.Rank,
    b.event_id, 
    b.result_race_id, 
    b.registration_id, 
    b.subclass_id
FROM (
        SELECT 
            if(@subclass_ID=A.subclass_id and @event_ID=a.event_ID,@Rank:=@rank+1,@Rank:=1) as Rank,
            @subclass_ID:=a.subclass_id,
            @event_ID:=a.event_ID,
            a.*
        FROM 
            ( 
                SELECT *
                FROM result_race_to_registration 
                ORDER BY event_id, subclass_id asc
            ) a
        ) b