mysql中每组的行号

时间:2013-07-30 05:51:37

标签: mysql

我希望根据这个可以根据crew_id和类型创建行号的场景生成查询结果。

id   crew_id   amount    type
 1      4       1000      AUB
 2      4       1500      AUB
 3      5       8000      CA
 4      4       1000      CA
 5      5       1000      AUB
 6      6       3000      AUB
 7      4       2000      CA
 8      6       3500      AUB
 9      4       5000      AUB
 10     5       9000      CA
 11     5       1000      CA

OUTPUT必须是ff:

id    crew_id   amount   type    row_number
 1      4       1000      AUB        1    
 2      4       1500      AUB        2 
 9      4       5000      AUB        3
 4      4       1000      CA         1
 7      4       2000      CA         2
 5      5       1000      AUB        1
 3      5       8000      CA         1
 10     5       9000      CA         2
 11     5       1000      CA         3
 6      6       3000      AUB        1
 6      6       3000      AUB        2

我只想在此输出中使用单个select语句

7 个答案:

答案 0 :(得分:44)

请通过我的小提琴

This One Last Tried

    SELECT    id,
              crew_id,
              amount,
              type,
             ( 
                CASE type 
                WHEN @curType 
                THEN @curRow := @curRow + 1 
                ELSE @curRow := 1 AND @curType := type END
              ) + 1 AS rank
    FROM      Table1 p,
              (SELECT @curRow := 0, @curType := '') r
   ORDER BY  crew_id,type asc;

答案 1 :(得分:17)

这个问题很老了。但是我想发布它以防有人遇到同样的问题。

首先,所描述的答案不正确。例如,对于

id   crew_id   amount    type
1      4       1000      AUB
2      4       1500      AUB
5      5       1000      AUB
6      6       3000      AUB
8      6       3500      AUB
9      4       5000      AUB

(我刚刚删除了类型为'CA'的行),结果表将是

id   crew_id   amount    rank   type
1      4       1000      1      AUB
2      4       1500      2      AUB
9      4       5000      3      AUB
5      5       1000      4      AUB
6      6       3000      5      AUB
8      6       3500      6      AUB

所以事实上它不使用crew_id和type,它只使用类型。

以下是我解决这个问题的方法(可能有一种比使用两个嵌套'CASE更优雅的方法,但你得到了这个想法):

SELECT id,
    amount,
    CASE crew_id 
        WHEN @curCrewId THEN
            CASE type 
                WHEN @curType THEN @curRow := @curRow + 1 
                ELSE @curRow := 1
            END
        ELSE @curRow :=1
    END AS rank,
    @curCrewId := crew_id AS crew_id,
    @curType := type AS type
FROM Table1 p
JOIN (SELECT @curRow := 0, @curCrewId := 0, @curType := '') r
ORDER BY crew_id, type

主要观点仍然存在。我刚刚添加了一个变量@curCrewId。如果有人需要使用3个变量进行分组,那么只需使用3个变量和3个嵌套的'CASE'。 :)

答案 2 :(得分:3)

当类型以数字开头时,@ Janty的答案不起作用(排名从2开始而不是1)。

请改用以下内容:

SELECT id,
     crew_id,
     amount,
     CASE type 
         WHEN @curType THEN @curRow := @curRow + 1 
         ELSE @curRow := 1
     END AS rank,
     @curType := type AS type
FROM Table1 p
JOIN (SELECT @curRow := 0, @curType := '') r
ORDER BY crew_id, type

答案 3 :(得分:2)

In addition to the answer of @Janty here is a solution if you want to UDATE your table with the rownumber:

UPDATE myTable mt,(SELECT @curRow := 0, @curType := '') r SET type=
    ( 
        CASE type 
            WHEN @curType
        THEN @curRow := @curRow + 1 
        ELSE @curRow := 1 AND @curType := type END
      )
;

As janty too crewId is not within. Use a second "case" for that as mentioned in the other answers.

答案 4 :(得分:2)

在 MySQL 8.0 中你可以使用窗口函数 ROW_NUMBER:

SELECT *, ROW_NUMBER() OVER(PARTITION BY crew_id) AS row_number
FROM MyTable

答案 5 :(得分:0)

SELECT id, crew_id, amount, type,
       ( 
        CASE type 
          WHEN @curType 
          THEN @curRow := @curRow + 1 
          ELSE @curRow := 1 AND @curType := type  END
      ) + 1 AS rank
 FROM      Table1 p,
      (SELECT @curRow := 0, @curType := '') r
   ORDER BY  crew_id, type asc;

答案 6 :(得分:0)

以下是我的回答,仅使用一个案例根据两列创建行号

SELECT id, crew_id, amount, type,
   (CASE CONCAT(crew_id, type) 
        WHEN @cur_crew_type
        THEN @curRow := @curRow + 1
        ELSE @curRow := 0 END) + 1 AS cnt,
    @cur_crew_type := CONCAT(crew_id, type) AS cur_crew_type
FROM TABLE t,
     (SELECT @curRow := 0, @cur_crew_type := '') counter
      ORDER BY crew_id, type;