mysql错误:1093-You can’t specify target table for update in FROM clause的解决方法

update语句中包含的子查询的表和update的表为同一张表时,报错:1093-You can’t specify target table for update in FROM clause

mysql不允许update目标表和子查询里面的表为同一张表

错误sql:
UPDATE mg_brand set `status`='0' where iID=(SELECT id from mg_industry where `name`='汽车') and id in (SELECT id from mg_brand WHERE nameC = '欧宝' or pID = (SELECT id from mg_brand WHERE nameC = '欧宝'));

解决办法:子查询sql可以改变双层的子查询,即可执行成功
示例sql:
UPDATE mg_brand SET `status` = '0' WHERE iID = ( SELECT id FROM mg_industry WHERE `name` = '汽车' ) AND id IN ( SELECT id FROM (SELECT id FROM mg_brand) AS temp WHERE nameC = '欧宝' OR pID = ( SELECT id FROM ( SELECT id FROM mg_brand WHERE nameC = '欧宝' ) AS te WHERE 1 ));

posted @ 2019-12-16 16:11  深海蓝精灵  阅读(3622)  评论(0编辑  收藏  举报