當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

【MySQL】把A表數(shù)據(jù)復(fù)制到B表中
2021-09-28 16:59:36

A表結(jié)構(gòu)

create table user_fans(
`id` int(11) unsigned not null auto_increment,
`user_id` int(11) not null,
`fans_id` int(11) not null,
primary key (`id`)
);

B表結(jié)構(gòu)

create table user_fans123(
`id` int(11) unsigned not null auto_increment,
`user_id` int(11) not null,
`fans_id` int(11) not null,
primary key (`id`)
);

復(fù)制表數(shù)據(jù)從A到B

insert into user_fans123 select * from user_fans;

這里有一份使用存儲過程批量生成數(shù)據(jù)來做演示

create procedure user_fans_procedure(out count int)
begin
declare i int;
set i = 1;
add_loop:loop
set i = i+1;
if i > 50 then
leave add_loop;
else
insert into user_fans (`user_id`,`fans_id`) values (i,I+1);
end if;
set count = i;
end loop add_loop;
select count;
end;

call user_fans_procedure(@count);

本文摘自 :https://blog.51cto.com/k

開通會員,享受整站包年服務(wù)立即開通 >