sql delete 嵌套 select查询 You can't specify target table 'student' for update in FROM clause

sql delete 嵌套 select查询 You can’t specify target table ‘student’ for update in FROM clause
#删除 除了 sid相同其它均相同,并且保存sid最大的元祖

     DELETE s FROM  student AS s WHERE 
      s.`sid`<> 
           (SELECT MAX(ss.sid) sid FROM student AS ss 
   WHERE  ss.`sname`=s.sname AND ss.`gender`=s.gender 
   AND ss.`class_id`=s.class_id )

内层嵌套的select子查询中,用到了外层delete查询的student表别名s,便会报此错误。
修改:
delete from student where sid not in (select a.sid from (select Min(sid) sid from student group by sname,gender,class_id) as a )

你可能感兴趣的:(数据库)