首页 > 开发 > MySQL > 正文

怎么把表A中数据分别导入到其他两张表中去?

2017-09-08 09:03:56  来源:网友分享
table A:name    name2a        hello1b        hello2c        hello3c        hello4d        hello5d        hello6将数据分别保存到两张表中去table B(id,name都是唯一的):id    name1    a2    b3    c4    dtable C:name2    idhello1    1hello2    2hello3    3hello4    3hello5    4hello6    4

解决方案

insert into b(id,name) select  (@i:=@i+1)as i,a.name  from (select DISTINCT name from a)a ,(select @i:=0) as tmp;insert into c(name2,id)SELECT a.name2,b.id from a,b where a.name=b.name;