我有 A、B、C 是三个不同的表,我需要从 A 表中删除在 B 和 C 表中都不存在的记录。
样本:
delete from A
where name NOT IN ( select name from B inner join C);
我有 A、B、C 是三个不同的表,我需要从 A 表中删除在 B 和 C 表中都不存在的记录。
样本:
delete from A
where name NOT IN ( select name from B inner join C);
我喜欢对这样的东西使用左连接。我首先创建一个这样的选择语句:
SELECT *
FROM TableA AS a
LEFT OUTER JOIN TableB AS b ON a.Name = b.Name
LEFT OUTER JOIN TableC AS c ON a.Name = c.Name
WHERE b.Name IS NULL
AND c.NAME IS NULL
然后我转换成这样的 DELETE 语句:
DELETE a
FROM TableA AS a
LEFT OUTER JOIN TableB AS b ON a.Name = b.Name
LEFT OUTER JOIN TableC AS c ON a.Name = c.Name
WHERE b.Name IS NULL
AND c.NAME IS NULL