我有一个查询。
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B WHERE j = 1
UNION select i from C
UNION select i from D
);
基本上删除A中的所有行,其中字段i
未出现在表B,C或D中。如果只是:
DELETE FROM A
WHERE i NOT IN
( SELECT i FROM B
);
然后可以使用左连接轻松完成
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i
WHERE B.id is NULL;
(假设每个表在模式中都有一个id字段)
我想我的问题是上面的问题延伸到了 具有以下解决方案的三表情景?
DELETE A FROM A
LEFT JOIN B
ON A.i = B.i AND B.j = 1
LEFT JOIN C
ON A.i = C.i
LEFT JOIN D
ON A.i = D.i
WHERE B.id is NULL
AND C.id is NULL
AND D.id is NULL
答案 0 :(得分:2)
这样的事情:
DELETE
FROM A
WHERE NOT EXISTS (
SELECT *
FROM B
WHERE B.i = A.i
)
AND NOT EXISTS (
SELECT *
FROM C
WHERE C.i = A.i
)
AND NOT EXISTS (
SELECT *
FROM D
WHERE D.i = A.i
);