从表中删除雇员名字相同的员工记录(需要最少保留一条记录)

表中数据:

执行语句:

mysql> delete from emp where empNO not in (select min(empNO) from emp group by ename);
ERROR 1093 (HY000): You can't specify target table 'emp' for update in FROM clause

MySQL出现You can’t specify target table for update in FROM clause
这个错误的意思是不能在同一个sql语句中,先select同一个表的某些值,
然后再update这个表。
这个错误在oracle中不会出现!


修改语句后执行:OK
delete from emp where empNO not in
(
select *
from
(
select min(empNO)
from emp
group by ename
)
a);

原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/7106847.html