我在两个表之间进行更新,例如:
Insert into TableA (Field1,Field2)
Select Field3,Field4 from TableB
where Field3 NOT IN (Select Field1 from TableA
我没有考虑到的是,这并没有检查来自TableB的欺骗,因此无论我的NOT IN如何,我仍然会添加重复项。
答案 0 :(得分:0)
在您的选择中的值前添加DISTINCT
。这将删除任何重复
INSERT INTO TableA (Field1,Field2)
SELECT DISTINCT Field3,Field4
FROM TableB
WHERE Field3 NOT IN (Select Field1 from TableA)
如果存在3个相同但在这种情况下不包含4的行使用GROUP BY
INSERT INTO TableA (Field1,Field2)
SELECT Field3,Field4
FROM TableB
WHERE Field3 NOT IN (Select Field1 from TableA)
GROUP BY Field3