[英]Insert different rows between Table A and Table B into Table B
I have two exactly identical tables. 我有两个完全相同的表。 Table A has eg 100 rows, Table B has 60 rows.
表A有例如100行,表B有60行。 Now I want to insert the "missing" rows from Table A into Table B. To find the different rows I would use:
现在,我想将表A中的“缺失”行插入表B中。要找到不同的行,我将使用:
select * from tableA where language = 4
union
select * from tableB where language = 4
How do I use this code correctly with the "Insert into" statement? 如何在“插入到”语句中正确使用此代码? My idea:
我的点子:
Insert into tableB
(Select * from tableA where language = 4
union
select * from tableB where language = 4)
Best regards 最好的祝福
UNION may be an option but it would cause duplications. UNION可能是一个选项,但会导致重复。 I would prefer MINUS operation.
我更喜欢减号操作。
insert into tableB (
select * from tableA where language=4
MINUS
select * from tableB where language=4)
Insert into tableB
(Select * from tableA where language = 4
EXCEPT
select * from tableB where language = 4)
Perhaps EXCEPT ALL
should be used, treats duplicates differently. 也许应该使用
EXCEPT ALL
,以不同的方式对待重复项。
Outer Join 外连接
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 ;
Not Exists 不存在
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);
Except 除了
INSERT INTO TableB (Column1,Column2)
SELECT Column1,
Column2
FROM TaBLE1
EXCEPT
SELECT Column1,
Column2
FROM #TaBLE2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.