试下这个sql是不是你想要的:insert into d (d1, d2, d3, d4, d5 ,来源表名) select b1, b2, b3, b4,null,'B' from b where exists(select 1 from a where a.a2=b.b2) union all select c1, c2 ,c3 ,c4,null,'C' from c where exists(select 1 from a where a.a2=c.c2)
insert into d (d1, d2, d3, d4, d5, 来源表名) select * from (select b1, b2, b3, b4, null b5, 'B' b6 from b where exists (select 1 from a where a.a2 = b.b2) union select c1, c2, c3, c4, null, 'C' from c where exists (select 1 from a where a.a2 = c.c2)) t where not exists (select 1 from d where d1 = t.b1 and d2 = t.b2 and d3 = t.b3 and d4 = t.b4 and 来源表名 = t.b6)
试下这个sql是不是你想要的:insert into d (d1, d2, d3, d4, d5 ,来源表名)
select b1, b2, b3, b4,null,'B'
from b
where exists(select 1 from a where a.a2=b.b2)
union all
select c1, c2 ,c3 ,c4,null,'C'
from c
where exists(select 1 from a where a.a2=c.c2)
(d1, d2, d3, d4, d5, 来源表名)
select *
from (select b1, b2, b3, b4, null b5, 'B' b6
from b
where exists (select 1 from a where a.a2 = b.b2)
union
select c1, c2, c3, c4, null, 'C'
from c
where exists (select 1 from a where a.a2 = c.c2)) t
where not exists (select 1
from d
where d1 = t.b1
and d2 = t.b2
and d3 = t.b3
and d4 = t.b4
and 来源表名 = t.b6)