简体   繁体   中英

Subquery fails in mysql

I have a subquery as follows : which will select the id's according to the condition first and delete

the records ,

    Delete from post_master_user_map WHERE id IN
(SELECT id FROM `post_master_user_map` WHERE posted_by_user_id=110);

But it gives me the following error :

You can't specify target table 'post_master_user_map' for update in FROM clause

What is wrong with this ? thanks in advance .

UPDATE

This also fails , I dont why

DELETE FROM `post_master_user_map` WHERE `reshare_id` in (SELECT id FROM `post_master_user_map` WHERE 
posted_by_user_id=110);

This error occurs when you try to modify a table and select from the same table in sub query.

Anyway to solve that error change your query as follows

Delete from post_master_user_map WHERE posted_by_user_id=110;

For the updated query(in your question) use following

    DELETE t1 FROM post_master_user_map as t1 INNER JOIN 
post_master_user_map as t2 ON t1.reshare_id=t2.id and t2.posted_by_user_id=110

通过此MySQL DELETE FROM以子查询为条件 :MySQL不允许在条件的子查询中使用要删除的表。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM