gpt4 book ai didi

mysql - 使用 mysql 的 sql 语法,它将按循环顺序返回直到最后一条记录

转载 作者:行者123 更新时间:2023-11-29 22:45:21 26 4
gpt4 key购买 nike

数据存储在表中

ID | STORE     | PRODUCT | PRICE
--------------------------------
1 | fliplart | Mobile | 20
2 | fliplart | x-box | 30
3 | fliplart | TV | 10
4 | amazon | Watch | 15
5 | amazon | x-box | 30
6 | ebay | TV | 10
7 | ebay | Watch | 15
8 | ebay | x-box | 30
9 | ebay | x-box | 30
10 | shopclues | TV | 10
11 | jabong | Watch | 15

想要循环排序存储数据,如下所示

ID | STORE     | PRODUCT | PRICE
--------------------------------
1 | fliplart | Mobile | 20
4 | amazon | Watch | 15
6 | ebay | TV | 10
9 | shopclues | TV | 10
10 | jabong | Watch | 15
2 | fliplart | x-box | 30
5 | amazon | x-box | 30
7 | ebay | Watch | 15
3 | fliplart | TV | 10
8 | ebay | x-box | 30
9 | ebay | x-box | 30

请根据需要帮助获取记录。

最佳答案

您可以通过为每个值添加“行号”然后按其排序来获得所需的内容:

select t.*
from (select t.*,
(@rn := if(@s = store, @rn + 1,
if(@s := store, 1, 1)
)
) as rn
from table t cross join
(select @s := '', @rn := 0) vars
order by store, id
) t
order by rn, store;

关于mysql - 使用 mysql 的 sql 语法,它将按循环顺序返回直到最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165720/

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