编程的魅力
首页
分类
标签
归档
动态
关于我
hyuga
2021-01-08
0
2021-01-08 14:34:07
原创
MySQL update where报错 You can't specify target table '表名' for update in FROM clause
# 前言 使用MySQL更新数据的时候,报错`You can't specify target table '表名' for update in FROM clause`,原因是在一条语句中,不能`select`出同一张表中的某些字段作为条件,再去`update`该表的字段。 # SQL 以下SQL报错: ``` update t_room set fexpand_id = null, fexpand_org_id = null, fexpand_position_id = null, fexpand_time = null where fid in( select r.fid from t_room r left join t_erp_person p on p.fid = r.fexpand_id where p.fposition_id = 'xxx' and r.fabandon_status = 'ENABLED' and r.froom_status = 'RENT' ); ``` 错误信息: `[HY000][1093] You can't specify target table 't_room' for update in FROM clause` 这种情况,把where条件中的数据集包装一下就好了,当中间表来用。 ``` update t_room set fexpand_id = null, fexpand_org_id = null, fexpand_position_id = null, fexpand_time = null where fid in( select a.fid from ( select r.fid from t_room r left join t_erp_person p on p.fid = r.fexpand_id where p.fposition_id = 'xxx' and r.fabandon_status = 'ENABLED' and r.froom_status = 'RENT' ) a ); ``` OK,执行成功!!!
MySQL
评论
发布
留言
评论