[英]insert into table without the intersection part
如果我有兩個SQL表,table_a和table_b相同,除了它們可能包含不同的數據,我想將table_b中已經不存在於table_a中的所有行插入到table_b中,查詢應該如何? 這些表僅包含id1和id2列。 如果我的問題不清楚,請告訴我
Insert into table_a ...
謝謝
; with cte_left as
(
select id1+id2
from table_a
where id1+id2 not in (select id1+id2 from table_b)
)
insert into table_b
select * from cte_left
您可以使用EXCEPT
運算符:
INSERT INTO table_a (id1, id2)
SELECT id1, id2
FROM (
SELECT id1, id2
FROM table_b
EXCEPT
SELECT id1, id2
FROM table_a
) except_gry
SQL Fiddle演示在這里
Insert into table_a ( id1, id2 )
select b.id1, b.id2
from table_b b
left outer join table_a a on a.id1 = b.id1 and a.id2 = b.id2
where a.id1 is null
您可以使用查詢中不存在的內容:
insert into table_a(id1,id2)
select id1,id2
from table_b
where not exists(select id1,id2 from table_a)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.