gpt4 book ai didi

mysql - 如何在指定时间重复 MySQL 结果集中的一行?

转载 作者:行者123 更新时间:2023-11-29 03:45:04 25 4
gpt4 key购买 nike

我有一个像这样的 MySQL 表:

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| id (int primary key) | count (int) |
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

假设此表填充有这些值:

1,1
2,2
3,2
4,3

我需要创建此表的 View ,将每一行重复为计数列的数量,例如对于上述数据, View 将包含以下信息:

1,1
2,1
2,2
3,1
3,2
4,1
4,2
4,3

最佳答案

drop table if exists my_test;

create table my_test (
id int not null auto_increment primary key,
`count` int)
engine = myisam;

insert into my_test (`count`) values (1),(2),(2),(3);

delimiter //
drop procedure if exists recurrences //
create procedure recurrences()
begin
declare a,b int;
declare i int default 1;
declare finite int default 0;
declare curs cursor for select id,`count` from my_test;
declare continue handler for not found set finite = 1;
drop table if exists tmp;
create temporary table tmp (id int,cnt int);
open curs;
my_loop:loop
fetch curs into a,b;
if finite = 1 then
leave my_loop;
end if;
while i <= b do
insert into tmp (id,cnt) values (a,i);
set i = i + 1;
end while;
set i = 1;
end loop;
close curs;
select * from tmp;
end //
delimiter ;

call recurrences();


+------+------+
| id | cnt |
+------+------+
| 1 | 1 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 4 | 1 |
| 4 | 2 |
| 4 | 3 |
+------+------+
8 rows in set (0.44 sec)

我首先想到的是,但我不喜欢执行时间。 ;)

关于mysql - 如何在指定时间重复 MySQL 结果集中的一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7051133/

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