小编典典

如何在MySQL中获取行ID

sql

我想获取不同idfeedback的行ID

SELECT  l.idfeedback_store,
    @curRow := @curRow + 1 AS row_number
FROM    feedback_store l
JOIN    (SELECT @curRow := 0) r;

结果

idfeedback_store        row_number
1                           1
1                           2
1                           3
2                           4
2                           5
2                           6
2                           7
3                           8
3                           9
3                          10
4                          11

所需结果

idfeedback_store        row_number
1                           1
1                           2
1                           3
2                           1
2                           2
2                           3
2                           4
3                           1
3                           2
3                           3
4                           1

阅读 308

收藏
2021-04-14

共1个答案

小编典典

试试这个:

SELECT  l.idfeedback_store, IF(@last=(@last:=idfeedback_store), @curRow := @curRow + 1, @curRow:=1) AS row_number
FROM feedback_store l, (SELECT @curRow := 0, @last:=0) r;
2021-04-14