2

我想获取不同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
4

3 回答 3

1

您可以使用类似的东西:

select idfeedback_store, group_row_number
from 
(
  select idfeedback_store,
    @num := if(@idfeedback_store = `idfeedback_store`, @num + 1, 1) as group_row_number,
    @idfeedback_store := `idfeedback_store` as dummy
  from feedback_store
) src

请参阅带有演示的 SQL Fiddle

结果:

| IDFEEDBACK_STORE | GROUP_ROW_NUMBER |
---------------------------------------
|                1 |                1 |
|                1 |                2 |
|                1 |                3 |
|                2 |                1 |
|                2 |                2 |
|                2 |                3 |
|                2 |                4 |
|                3 |                1 |
|                3 |                2 |
|                3 |                3 |
|                4 |                1 |
于 2012-12-21T12:59:08.057 回答
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;
于 2012-12-21T13:03:45.190 回答
0
SELECT  
  idfeedback_store,
  @curRow := CASE WHEN @curStore = idfeedback_store THEN @curRow + 1 ELSE 1 END AS row_number,
  @curStore := idfeedback_store
FROM
  feedback_store;
于 2012-12-21T12:58:46.340 回答