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

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

DELETE FROM student WHERE ID  in
(SELECT ID FROM student where 1 = 1 and 
(srudent.name= 'X' or student.name= 'Y'))

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

原因是子查询中使用student表进行查询,而外层的delete语句也是对student进行的操作,因此报错。

解决办法:

将子查询再包装一次:

DELETE FROM student WHERE ID  in
(SELECT * FROM (SELECT ID FROM student where 1 = 1 and 
(student.name= 'X' or student.name= 'Y')) a)

注意:这种错误只出现在mySQL中。

参考:https://www.cnblogs.com/HapLe0/p/7761016.html

抬走,下一个坑!!!

posted @ 2022-10-31 17:05  厚我  阅读(49)  评论(0编辑  收藏  举报