mysql删除是报You can‘t specify target table ‘表名‘ for update in FROM clause

mysql删除是报You can‘t specify target table ‘表名‘ for update in FROM clause

 

DELETE FROM evaluation_classification  WHERE id IN (
SELECT id FROM evaluation_classification WHERE parent_id=999)

  以上SQL语句会报错。原来是因为  在mysql中,不支持先 select 一个表的记录,在按此条件进行更新和删除同一个表的记录。

  解决办法是:将select得到的结果,再通过中间表select一遍,这样就可以规避错误,这个问题只出现于mysql。oracle 并不会出现。

       修改后的SQL语句为如下

       

DELETE FROM evaluation_classification WHERE id IN (
SELECT a.id FROM (
SELECT id FROM evaluation_classification WHERE parent_id=999) a)

 

  

 

posted @ 2022-01-19 17:46  xlisteven  阅读(267)  评论(0)    收藏  举报