此请求有效:
有两个连续的FROM。 当我删除第一个时,为什么不起作用?
DELETE FROM TableA
FROM TableA dim
LEFT OUTER JOIN (
SELECT DISTINCT ColA
FROM TableB
UNION ALL
SELECT DISTINCT ColA
FROM tableC
) A ON A.ColA= dim.ColA
WHERE A.ColA IS NULL
编辑 我认为这是不正确的,这是正确的:
DELETE
FROM TableA dim
LEFT OUTER JOIN (
SELECT DISTINCT ColA
FROM TableB
UNION ALL
SELECT DISTINCT ColA
FROM tableC
) A ON A.ColA= dim.ColA
WHERE A.ColA IS NULL
谢谢
答案 0 :(得分:5)
这是因为表联接。 SQL引擎需要知道要从哪个源中删除,而奇数FROM FROM
语法可以完成此任务。
您拥有的语句在功能上等效于此语句,除了将表别名替换为第一个FROM
子句之外,该语句完全相同:
DELETE dim
FROM TableA dim
LEFT OUTER JOIN (
SELECT DISTINCT ColA
FROM TableB
UNION ALL
SELECT DISTINCT ColA
FROM tableC
) A ON A.ColA= dim.ColA
WHERE A.ColA IS NULL
答案 1 :(得分:0)
您可以使用NOT EXISTS
:
DELETE dim
FROM TableA dim
WHERE NOT EXISTS (SELECT 1 FROM TableB B WHERE B.ColA = DIM.ColA) OR
NOT EXISTS (SELECT 1 FROM TableC C WHERE C.ColA = DIM.ColA);
您的delete语句有两个不正确的from
子句,它在delete
语句之后应该只有一个立即数:
DELETE dim
FROM TableA dim LEFT OUTER JOIN
(SELECT ColA
FROM TableB
UNION
SELECT ColA
FROM tableC
) A
ON A.ColA= dim.ColA
WHERE A.ColA IS NULL;
此外,如果要输入DISTINCT
,然后使用UNION ALL
,那么UNIQUE
和UNION
就是多余的。