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

Posted testeyes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 

以上是关于Mysql更新数据时,报 “You can't specify target table 'message ' for update in FROM clause” 解决办法的主要内容,如果未能解决你的问题,请参考以下文章

mySQL: delete 语句报错 You can't specify target table 'student' for update in FROM clause(示例

mysql uodate 报错 You can't specify target table '**' for update in FROM clause

mysql - sql报错You can't specify target table 'table_name' for update in FROM clause

使用mysqlclient执行存储过程报错2014 Commands out of sync; you can't run this command now

MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法

Mysql -- You can't specify target table 'address' for update in FROM clause