Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MySQL结果集中重复一行指定的时间?_Mysql_Set_Record_Resultset_Repeat - Fatal编程技术网

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

如何在MySQL结果集中重复一行指定的时间?,mysql,set,record,resultset,repeat,Mysql,Set,Record,Resultset,Repeat,我有一个MySQL表,如下所示: -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | id (int primary key) | count (int) | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 假设此表中填充了以下值: 1,1 2,2 3,2 4,3 我需要创建此表的视图,该视图将每一行作为计数列的数量重复,例如,对于上述数据,该视图将包含以下信息: 1,1 2,1 2,2 3,1 3,2 4,1 4,2 4,3 如

我有一个MySQL表,如下所示:

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| id (int primary key) | count (int) |
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
假设此表中填充了以下值:

1,1
2,2
3,2
4,3
我需要创建此表的视图,该视图将每一行作为计数列的数量重复,例如,对于上述数据,该视图将包含以下信息:

1,1
2,1
2,2
3,1
3,2
4,1
4,2
4,3
如果存在my_测试,则删除表;
创建表my_测试(
id int not null自动递增主键,
`计数(整数)
发动机=myisam;
在my_测试(`count`)中插入值(1)、(2)、(2)、(3);
分隔符//
如果存在重复,则删除程序//
创建过程重现()
开始
声明a,b int;
声明i int默认值为1;
声明有限整数默认值为0;
为my_测试中的select id“count”声明游标;
声明未找到集合的continue处理程序finite=1;
如果存在tmp,则删除表格;
创建临时表tmp(id int,cnt int);
开放游标;
我的循环:循环
把诅咒带进a,b;
如果有限=1,则
离开我的循环;
如果结束;
当我
删除表格时,如果存在我的_测试;
创建表my_测试(
id int not null自动递增主键,
`计数(整数)
发动机=myisam;
在my_测试(`count`)中插入值(1)、(2)、(2)、(3);
分隔符//
如果存在重复,则删除程序//
创建过程重现()
开始
声明a,b int;
声明i int默认值为1;
声明有限整数默认值为0;
为my_测试中的select id“count”声明游标;
声明未找到集合的continue处理程序finite=1;
如果存在tmp,则删除表格;
创建临时表tmp(id int,cnt int);
开放游标;
我的循环:循环
把诅咒带进a,b;
如果有限=1,则
离开我的循环;
如果结束;

虽然我在数据库中这样做会很笨拙。。。只需在SQL之外的表示层上执行此操作。在数据库中执行此操作将非常笨拙。。。只需在SQL之外的表示层上执行此操作。谢谢您的回答,但正如我所说的,我需要一个视图而不是一个存储过程来执行此操作!我认为不在存储过程中创建视图是不可能的。谢谢你的回答,但正如我所说的,我需要一个视图而不是一个存储过程来完成这项工作!我认为不在存储过程中创建视图是不可能的。
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)