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;