delete语句中,子查询语句不好用如何解决

如果需要在delete的时候,按照条件删除记录,那么可能会用到子查询。

但是如下情况:

delete from student where id = (select max(id) from student);
[Err] 1093 - You can't specify target table 'student' for update in FROM clause


解决办法是:给 from 子句中的结果集起别名。

delete from student where id = (select n.max_id from (select max(id) as max_id from student) as n);


这个方法对in还是适用的

delete from student where id in (select id from student where id > 30);
[Err] 1093 - You can't specify target table 'student' for update in FROM clause


解决方法同上:

delete from student where id in (select n.id from (select id from student where id > 30) as n);

转:https://www.cnblogs.com/dwarcheng/p/5887207.html









你可能感兴趣的:(delete语句中,子查询语句不好用如何解决)