如何从员工表中删除收入最高的员工?

时间:2019-11-03 13:58:55

标签: mysql

Employee 表由Employee_id,雇员姓名和薪水组成

如何从员工表中删除收入最高的员工? 我尝试过

delete from employee
where salary = (select max(salary) from employee);

但是它给出了一个错误;

  

错误代码:1093。您不能指定目标表'employee'进行更新   在FROM子句中0.0022秒

3 个答案:

答案 0 :(得分:0)

您只需完成

delete from employee 
order by salary desc
limit 1

或者修正您的查询

delete from employee 
where salary in 
(
  select * from (select max(salary) from employee) x
)

因为您需要构建临时表作为解决方法,因为您需要在同一表中进行选择并删除。

答案 1 :(得分:0)

把它埋得更深

delete from employee 
where salary = (select maxsalary from (select max(salary) maxsalary from employee) s );

这将删除所有具有最高薪水的员工

答案 2 :(得分:-1)

这应该做。

delete from employee where salary = (select max(salary) from employee);