Want Row Number on Group of column in MY SQL?

后端 未结 5 1865
不思量自难忘°
不思量自难忘° 2020-12-20 19:34

I have one table in MYSQL It has data like

Id Name 
1  test
1  test
1  test123
2  test222
3  test333

I want the data like

Id Name     RowN         


        
5条回答
  •  时光取名叫无心
    2020-12-20 20:18

    Here's another way with a query that is simpler (at least for me):

    SELECT
    a.Id, a.Name,
    (SELECT COUNT(*) FROM test
     WHERE Id = a.Id AND `Name` = a.Name AND row_id < a.row_id) AS RowNum
    FROM test AS a
    ORDER BY a.row_id;
    

    This supposes there exists a global row_id (for instance an auto-increment primary key in your table).

提交回复
热议问题