gpt4 book ai didi

mysql - 自定义行编号

转载 作者:太空宇宙 更新时间:2023-11-03 11:05:00 25 4
gpt4 key购买 nike

我希望在从表中选择时对数据行进行编号。问题是,我不需要顺序编号,但它应该从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com