|
第一种方式:
insert into 表4
select …… from 表1
union
select …… from 表2
union
…………
select …… from 表n
如果觉得这样仍然有重复数据,可以考虑嵌套一下,加上distinct或者group by
例如:
insert into 表4
select distinct id,字段1,字段2……from (
select …… from 表1
union
select …… from 表2
union
…………
select …… from 表n) a
-------------------------------这里是分割线-------------------------------
第二种方式:
步骤1:插入不重复的基准数据。
insert into 表4(sid)
select id from 表1
union
select id from 表2
union
……
select id from 表n
步骤2:更新必要的字段
update 表4 set 表4.字段1=表1.字段1
from 表1 where 表4.sid=表1.id
update 表4 set 表4.字段2=表1.字段2
from 表1 where 表4.sid=表1.id
……
update 表4 set 表4.字段n=表1.字段n
from 表1 where 表4.sid=表1.id
直至把表4需要更新的字段更新完毕
步骤3:对表2,表3……表n重复步骤2,直至把所有表的字段更新完毕。
|
|