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

Posted zhangxuezhi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql - sql报错You can't specify target table 'table_name' for update in FROM clause相关的知识,希望对你有一定的参考价值。

 

今天写了个更新数据库表的语句,本意是根据某个条件,筛选出该表某些数据之后,对这些数据进行删除操作,如下

delete from t_person where id in (select id from t_person where name = "hello");

然而却报错: You can‘t specify target table ‘t_person‘ for update in FROM clause

以下这篇博客( https://blog.csdn.net/qq_29672495/article/details/72668008 )给出了解决方式: 将SELECT出的结果再通过中间表SELECT一遍,这样就规避了错误。

更正如下:

delete from t_person where id in (select temp.id from (select id from t_person where name = "hello") temp);

 

以上是关于mysql - sql报错You can't specify target table 'table_name' for update in FROM clause的主要内容,如果未能解决你的问题,请参考以下文章

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中You can't specify target table for update in FROM clause

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

mysql You can't specify target table 'xxx' for update in FROM clause的解决

MySQL sql 报错 You have an error in your SQL syntax; check the manual that corresponds to your MySQ