如何在表之间传输数据,仅在id不存在的情况下
How to transfer data between tables, only where id does not exist
我需要从表A复制一组数据到表B,像这样:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
上面的内容可以很好地工作,但是 TableB
可能已经包含一些我需要复制的记录,由 PK id
标识。
因此,如果 id
值不在表 B 中,我如何才能向 添加一个子句,仅 插入一条记录?我知道可以在 INSERT 语句的末尾添加一个 WHERE
子句,但我不确定如何将它应用于每条记录。
您可以使用左连接,如下所示
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
left join TableB on TableB.id = TableA.id
where TableB.id is null
您可能需要一个带有 exists() 的 where 子句
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA a
WHERE not exists (SELECT 1 FROM TableB b WHERE a.id = b.id)
INSERT INTO TableB(id,field1,field2)
SELECT A.id,A.field1,A.field2 FROM TableA WHERE NOT EXISTS
(SELECT B.ID FROM TABLEB WHERE B.ID = A.ID)
尝试使用左连接:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA left join TableB on tableA.id = tableb.id where tablea.id is null
你可以看看比较 NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL.
的 3 种方法
The best way to search for missing values in MySQL is using a LEFT
JOIN / IS NULL or NOT IN rather than NOT EXISTS.
您可以使用 NOT EXISTS.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE NOT EXISTS (
SELECT *
FROM TableB t2
WHERE t1.id = t2.id
)
您也可以使用 LEFT JOIN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
LEFT JOIN TableB t2 ON t1.id = t2.id
WHERE t2.id IS NULL
您也可以使用 NOT IN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE t1.id NOT IN (
SELECT t2.id
FROM TableB t2
WHERE t1.id = t2.id
)
最好使用 set operator EXCEPT 以获得最佳性能,如下所示:
此处将取两组数据进行减法运算并给出输出
INSERT INTO TableB(id,field1,field2) (
SELECT id,field1,field2 FROM TableA
except
SELECT id,field1,field2 FROM TableB )
INSERT INTO TableB(id,field1,field2)
SELECT T2.id,T2.field1,T2.field2 FROM TableA as T1
在 T1.id <> T2.id
上作为 T2 内部加入 TableB
我需要从表A复制一组数据到表B,像这样:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
上面的内容可以很好地工作,但是 TableB
可能已经包含一些我需要复制的记录,由 PK id
标识。
因此,如果 id
值不在表 B 中,我如何才能向 添加一个子句,仅 插入一条记录?我知道可以在 INSERT 语句的末尾添加一个 WHERE
子句,但我不确定如何将它应用于每条记录。
您可以使用左连接,如下所示
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA
left join TableB on TableB.id = TableA.id
where TableB.id is null
您可能需要一个带有 exists() 的 where 子句
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA a
WHERE not exists (SELECT 1 FROM TableB b WHERE a.id = b.id)
INSERT INTO TableB(id,field1,field2)
SELECT A.id,A.field1,A.field2 FROM TableA WHERE NOT EXISTS
(SELECT B.ID FROM TABLEB WHERE B.ID = A.ID)
尝试使用左连接:
INSERT INTO TableB(id,field1,field2)
SELECT id,field1,field2 FROM TableA left join TableB on tableA.id = tableb.id where tablea.id is null
你可以看看比较 NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL.
的 3 种方法The best way to search for missing values in MySQL is using a LEFT JOIN / IS NULL or NOT IN rather than NOT EXISTS.
您可以使用 NOT EXISTS.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE NOT EXISTS (
SELECT *
FROM TableB t2
WHERE t1.id = t2.id
)
您也可以使用 LEFT JOIN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
LEFT JOIN TableB t2 ON t1.id = t2.id
WHERE t2.id IS NULL
您也可以使用 NOT IN.
INSERT INTO TableB (id, field1, field2)
SELECT id, field1, field2
FROM TableA t1
WHERE t1.id NOT IN (
SELECT t2.id
FROM TableB t2
WHERE t1.id = t2.id
)
最好使用 set operator EXCEPT 以获得最佳性能,如下所示:
此处将取两组数据进行减法运算并给出输出
INSERT INTO TableB(id,field1,field2) (
SELECT id,field1,field2 FROM TableA
except
SELECT id,field1,field2 FROM TableB )
INSERT INTO TableB(id,field1,field2) SELECT T2.id,T2.field1,T2.field2 FROM TableA as T1 在 T1.id <> T2.id
上作为 T2 内部加入 TableB