1

我正在尝试删除记录joinstables但它不起作用。

我的查询是:

DELETE FROM category
WHERE  catid NOT IN(SELECT av.catid
                    FROM   category av
                           JOIN rel
                             ON rel.catid = av.catid
                           JOIN main_list
                             ON rel.webid = main_list.mainid
                    GROUP  BY av.catid)  

为什么这个查询不起作用?它抛出以下错误:

#1093 - You can't specify target table 'category' for update in FROM clause

我该如何解决?我不确定我做错了什么错误。

4

2 回答 2

6

尝试使用LEFT JOIN.

DELETE  av
FROM    category av
        LEFT JOIN rel ON rel.catid = av.catid
        LEFT JOIN main_list ON rel.webid = main_list.mainid 
WHERE   rel.catid IS NULL

请在执行此查询之前备份您的数据库。

于 2013-07-05T11:54:09.530 回答
1

试试这个::

DELETE c from  category c
LEFT join rel  ON rel.catid = av.catid
LEFT JOIN main_list  ON rel.webid = main_list.mainid
where rel.catid is null 

或者

DELETE FROM category
WHERE  catid NOT IN(
Select catid from 
(SELECT av.catid as catid
                    FROM   category av
                           JOIN rel
                             ON rel.catid = av.catid
                           JOIN main_list
                             ON rel.webid = main_list.mainid
                    GROUP  BY av.catid)  as tempCat)
于 2013-07-05T11:54:26.810 回答