【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

insert into user_fans123 select * from user_fans;

這裏有一份使用存儲過程批量生成數據來作演示

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);
相關文章
相關標籤/搜索