我有一个子查询如下:首先根据条件选择id并删除
记录,
Delete from post_master_user_map WHERE id IN
(SELECT id FROM `post_master_user_map` WHERE posted_by_user_id=110);
但它给了我以下错误:
You can't specify target table 'post_master_user_map' for update in FROM clause
这有什么问题?提前谢谢。
更新
这也失败了,我不知道为什么
DELETE FROM `post_master_user_map` WHERE `reshare_id` in (SELECT id FROM `post_master_user_map` WHERE
posted_by_user_id=110);
答案 0 :(得分:1)
当您尝试修改表并从子查询中的同一个表中进行选择时,会发生此错误。
无论如何要解决该错误,请按以下方式更改您的查询
Delete from post_master_user_map WHERE posted_by_user_id=110;
对于更新的查询(在您的问题中),请使用以下
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
答案 1 :(得分:0)
通过这个MySQL DELETE FROM with subquery as condition:MySQL不允许在子查询中使用您要删除的表。