且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

连接两个不同的表并删除重复的条目

更新时间:2022-04-08 08:59:36

您可以使用 UNION 子句, UNION 将检查重复项,并且仅返回不同的行

You can use UNION clause, UNION will check for duplicates and only distinct rows will be returned

SELECT * FROM table1
UNION
SELECT * FROM Table2

编辑:要存储两个表中的数据而不重复,请执行以下操作

To store data from both table without duplicates, do this

INSERT INTO TABLE1
SELECT * FROM TABLE2 A
WHERE NOT EXISTS (SELECT 1 FROM TABLE1 X 
                  WHERE A.NAME = X.NAME AND 
                  A.post_code = x.post_code)

这将从表2中插入与名称不匹配的行,表1中的邮政编码

This will insert rows from table2 that do not match name, postal code from table1

或者,您也可以创建新表,而不触摸table1和table2

Alternative is that You can also create new table and not touch table1 and table2

CREATE TABLE TABLENAME AS
SELECT * FROM table1
UNION
SELECT * FROM Table2