如果我有两个SQL表,table_a和table_b,两者都是相同的,除了它们可能包含不同的数据,我想在table_b中插入table_b中不存在的所有行,如何查询看起来像 ?这些表仅包含id1和id2列。 如果我的问题不明确,请告诉我
Insert into table_a ...
谢谢
答案 0 :(得分:0)
; 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
答案 1 :(得分:0)
您可以使用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小提琴演示here
答案 2 :(得分:0)
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
答案 3 :(得分:0)
您的查询中不存在:
insert into table_a(id1,id2)
select id1,id2
from table_b
where not exists(select id1,id2 from table_a)