【问题标题】:How to delete the highest paid employee from employee table?如何从员工表中删除最高薪员工?
【发布时间】:2019-11-03 13:58:55
【问题描述】:

Employee表由Employee_id、Employee name和salary组成

如何从员工表中删除薪酬最高的员工? 我试过了

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

但它给出了一个错误;

错误代码:1093。您无法指定目标表“员工”进行更新 在 FROM 子句中 0.0022 秒

【问题讨论】:

  • 如果他们共享相同的高薪,这(如果它符合您的要求)将删除多个员工。这是故意的吗?

标签: mysql


【解决方案1】:

你可以这样做

delete from employee 
order by salary desc
limit 1

或修复您的查询

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

因为您需要构建一个临时表作为解决方法,因为您从同一个表中选择和删除。

【讨论】:

  • 第二个命令显示错误:错误代码:1175。您正在使用安全更新模式,并且您尝试更新没有使用 KEY 列的 WHERE 的表。
【解决方案2】:

把它埋得更深一点

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

这将删除所有最高工资的员工

【讨论】:

  • 我们为什么要把它埋得更深?
【解决方案3】:

应该这样做。

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

【讨论】:

  • 此错误 ERROR 1093 (HY000): Table 'employees' 被指定了两次,既作为 'DELETE' 的目标,又作为数据的单独来源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2011-11-17
  • 1970-01-01
  • 2019-11-02
  • 2015-11-20
相关资源
最近更新 更多