0

此查询准确返回我需要删除的办公室元组。

    SELECT id, reference FROM office o 
WHERE o.id NOT IN 
    (SELECT c.office_id FROM Contract c  WHERE office_id IS NOT NULL)
AND o.reference IN 
    (SELECT o.reference FROM Contract c JOIN office o ON c.office_id = o.id  WHERE office_id IS NOT NULL);

但是当我写查询

    DELETE FROM office 
WHERE id NOT IN 
    (SELECT c.office_id FROM Contract c  WHERE office_id IS NOT NULL)
AND reference IN 
    (SELECT o.reference FROM Contract c JOIN office o ON c.office_id = o.id  WHERE office_id IS NOT NULL);

我有以下错误:#1093 - You can't specify target table 'office' for update in FROM clause

但我真的不知道如何解决这个问题..

任何帮助将非常感激!

4

1 回答 1

1

尝试

 DELETE FROM office 
 WHERE id NOT IN 
(SELECT c.office_id FROM Contract c
 WHERE office_id IS NOT NULL)
 AND reference IN 
(select * from (SELECT o.reference FROM Contract c 
 JOIN office o ON c.office_id = o.id 
 WHERE office_id   IS NOT NULL) x);

因为您无法从您选择的表中删除。但是使用

select * from (select col from your_table) x

应该做的伎俩

于 2012-10-06T23:54:02.790 回答