KTM
KTM

Reputation: 868

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);

Upvotes: 0

Views: 67

Answers (2)

Fathah Rehman P
Fathah Rehman P

Reputation: 8761

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

Upvotes: 1

viktarpunko
viktarpunko

Reputation: 353

By this MySQL DELETE FROM with subquery as condition : MySQL does not allow the table you're deleting from be used in a subquery for the condition.

Upvotes: 0

Related Questions