Row number per group in mysql

前端 未结 6 556
臣服心动
臣服心动 2020-11-27 17:27

I want to produce query result base on this scenario that can create row number according to crew_id and type.

id   crew_id   amount    type
 1      4                


        
6条回答
  •  隐瞒了意图╮
    2020-11-27 18:12

    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.

提交回复
热议问题