Mysql更新数据时,报 “You can't specify target table 'message ' for update in FROM clause” 解决办法

报错的SQL为:

delete from message where id not in  (select min(id) as id from message group by content)

 

报错信息的中文意思为:不能在FROM子句中更新  message表数据,原因是 "不能在从本表中查出数据范围后,然后直接更新本表",

解决方法思路是,让数据库认为,你查出的数据范围,是不从本表中查出来的,实现步骤如下:

1、用as,给查出的数据表,设置别名为a,即 (select min(id) as id from message group by content)as a

2、用别名,查出数据范围,即 select a.id from (select min(id) as id from message group by content)as a

 

修改后的完整SQL为

delete from message where id not in  (select a.id from (select min(id) as id from message group by content) as a)

 

posted on 2019-08-27 16:37  测试眼  阅读(325)  评论(0编辑  收藏  举报