我有两个完全相同的表格。表A具有例如100行,表B有60行。现在我想将表A中的“缺失”行插入表B. 要查找我将使用的不同行:
select * from tableA where language = 4
union
select * from tableB where language = 4
如何使用“插入到”语句正确使用此代码? 我的想法:
Insert into tableB
(Select * from tableA where language = 4
union
select * from tableB where language = 4)
祝你好运
答案 0 :(得分:2)
UNION可能是一种选择,但它会导致重复。我更喜欢MINUS操作。
insert into tableB (
select * from tableA where language=4
MINUS
select * from tableB where language=4)
答案 1 :(得分:0)
Insert into tableB
(Select * from tableA where language = 4
EXCEPT
select * from tableB where language = 4)
可能应该使用EXCEPT ALL
,以不同方式处理重复项。
答案 2 :(得分:0)
外部加入
INSERT INTO TableB (Column1,Column2)
SELECT DISTINCT
a.Column1 ,
a.Column2 ... more column
FROM TableA AS a
LEFT OUTER JOIN TableB AS b ON b.Column1 = a.Column1
AND b.Column2 = a.Column2
WHERE b.Colum2 IS NULL AND b.Colum1 IS NULL ;
不存在
INSERT INTO TableB (Column1,Column2)
SELECT a.Column1 ,
a.Column2...
FROM Table1 AS a
WHERE NOT EXISTS ( SELECT *
FROM Table2 AS b
WHERE b.Column1 = a.Column1
AND b.Column2= a.Column2);
<强>除强>
INSERT INTO TableB (Column1,Column2)
SELECT Column1,
Column2
FROM TaBLE1
EXCEPT
SELECT Column1,
Column2
FROM #TaBLE2;