mysql执行报异常You can't specify target table '' for update in FROM clause

update t_area t set t.parent_name = (select t2.name from t_area t2 where t.parent_id = t2.id);

如果用户使用如上的sql语句就会报错,因为同一张表即作为被更新的对象,又作为被查询的对象
如果一定要使用这样的语句,可以尝试以下写法:

update t_area t set t.parent_name = (select t2.name from (select * from t_area) t2 where t.parent_id = t2.id);

效率可能会比较低

posted on 2019-07-05 09:58  Mrnx  阅读(286)  评论(0编辑  收藏  举报