/* [Err] 1093 - You can't specify target table 't' for update in FROM clause */
update t_area set parent_name = (select name from t_area where parent_id = id);
执行SQL语句时出现这个错误。原因是在更新这个表和数据时又查询了它,而查询的数据又做了更新的条件。以前ORACLE上面没有这个错误。
解决方法:
把要更新的几列数据查询出来做为一个第三方表,然后筛选更新。
--ok
update t_area t set t.parent_name = (select t2.name from (select * from t_area) t2 where t.parent_id = t2.id);
参考资料:
1、MySQL - 1093异常 - You can’t specify target table ‘t’ for update in FROM clause
https://www.cnblogs.com/nick-huang/p/4412818.html
2、 [Err] 1093 - You can’t specify target table ‘s’ for update in FROM clause
https://blog.csdn.net/yaerfeng/article/details/7876461
3、You can’t specify target table ‘NAME’ for update in FROM clause
https://stackoverflow.com/questions/17742214/you-cant-specify-target-table-name-for-update-in-from-clause