我想从表中选择后对数据行进行编号。问题是,我不需要顺序编号,但是应该从1到3依次编号,直到最后,如下所示:
1 | first row
2 | second row
3 | third row
1 | fourth row
2 | and
3 | ....
1
2
3
我正在尝试此查询,但无法正常工作:
mysql -> SET @n = 0;
-> SELECT
CASE
WHEN nnn = 3 THEN @n := 0
ELSE nnn
END
FROM (
SELECT @n := @n + 1 AS nnn FROM mytable
) AS t;
如何使其正常工作?
最佳答案
根据以下答案改编:With MySQL, how can I generate a column containing the record index in a table?
SELECT MOD(@curRow := @curRow + 1, 3) AS row_number
FROM mytable m
JOIN (SELECT @curRow := 0) r;
关于mysql - 自定义行编号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12459340/