0

我想从连接构造中删除,我必须为其提供别名(“映射”),因为最后我还必须在连接上使用 EXISTS 子句。所以整个事情看起来像这样:

DELETE a
FROM (TableA a INNER JOIN 
        (SELECT * FROM TableX x INNER JOIN TableY y ON x.id = y.id) map
      ON a.key = map.key) mapped
WHERE EXISTS
      (SELECT * 
       FROM  LookUp l 
       WHERE l.key1 = mapped.TableAKey 
       AND   l.key2 = mapped.TableXKey
       AND   l.key3 = mapped.TableYKey)

问题似乎与括号有关,因为我收到一个错误:

Incorrect syntax near 'mapped'. 

任何帮助,将不胜感激。

4

2 回答 2

0

我相信 DELETE 语句需要引用 FROM 子句中使用的别名。由于您的别名是“映射”的,请尝试按如下方式更改 DELETE:

DELETE mapped
FROM (TableA a INNER JOIN 
        (SELECT * FROM TableX x INNER JOIN TableY y ON x.id = y.id) map
      ON a.key = map.key) mapped
WHERE EXISTS
      (SELECT * 
       FROM  LookUp l 
       WHERE l.key1 = mapped.TableAKey 
       AND   l.key2 = mapped.TableXKey
       AND   l.key3 = mapped.TableYKey)
于 2013-10-28T11:00:52.850 回答
0

只需重写它,以便从显式表中删除并处理 where 子句中的所有条件,就像这样。

DELETE TableA
WHERE
    EXISTS (
        SELECT * 
        FROM 
            TableX x 
            INNER JOIN TableY y ON x.id = y.id
        WHERE
            x.key = TableA.key and
            EXISTS (
                SELECT * 
                FROM  LookUp l 
                WHERE l.key1 = TableA.TableAKey 
                AND   l.key2 = x.TableXKey
                AND   l.key3 = y.TableYKey
            )
    )

DELETE TableA另请注意,在处理 where 子句时替换为可能会有所帮助,SELECT * FROM TableA以准确查看将要删除的记录。

于 2013-10-28T12:27:30.573 回答