简体   繁体   English

子查询在mysql中失败

[英]Subquery fails in mysql

I have a subquery as follows : which will select the id's according to the condition first and delete 我有一个子查询,如下所示:它将首先根据条件选择ID并删除

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 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不允许在条件的子查询中使用要删除的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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