mysql 1093 - You can't specify target table 'xx表' for update in FROM

出现这个错误的原因是,在更新某表时,不能在子查询中查询同一张表。官方文档中有对应表述,“Currently, you cannot update a table and select from the same table in a subquery.”,
见http://dev.mysql.com/doc/refman/5.5/en/update.html。
解决这个问题的办法就是,将子查询的结果集当作临时表,性能较差,仅仅适合较小的数据量:

SQL:
UPDATE contract_detial i
SET
i.CONTRACT_NO = "123"
where
i.DETAILPKID in (SELECT * from (SELECT n.DETAILPKID from contract_detial n where n.PKID = "125973") as n);

以上操作同样适用于删除语句。

参考这位老哥的文章:https://www.cnblogs.com/nick-huang/p/4412818.html

posted @ 2020-09-09 16:42  iHADream  阅读(193)  评论(0编辑  收藏  举报