Monday 3 September 2018

Mysql - The delete query does not work with joins advertisements

I'm trying to delete records by joins of tables but it is not working.

My query is :
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)

Why is this query not working? It's throwing the following error:
#1093 - You can't specify target table 'category' for update in FROM clause
How do I fix this? I'm not sure what I'm doing wrong by the error.

try using 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

Please backup your database before executing this query.

0 comments:

Post a Comment