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);

猜你喜欢

转载自www.cnblogs.com/zhangxuezhi/p/11593927.html