天天看点

【MySQL】把A表数据复制到B表中

A表结构

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表结构

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`)
);
           

复制表数据从A到B

这里有一份使用存储过程批量生成数据来做演示

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);

           

继续阅读