我目前正在自学mysql,并试图从表中删除收入最低的员工,但是我不断收到此错误,我不了解或不知道如何解决。
delete from Employee
where Salary = (select min(Salary) from Employee)
有人可以帮我吗?
答案 0 :(得分:1)
在MySQL中,您不能修改表并在子查询中从同一表中选择。这适用于诸如DELETE,INSERT,REPLACE,UPDATE和(因为子查询可以在SET子句中使用)LOAD DATA之类的语句。
尝试在查询之前设置最低工资记录,例如:
SET @min = (SELECT MIN(Salary) FROM Employee);
DELETE FROM Employee WHERE Salary=@min LIMIT 1;
或者您甚至可以通过创建another subquery来完全避免第一个查询:
DELETE FROM Employee
WHERE Salary=(
SELECT
minSalary
FROM (SELECT MIN(Salary) AS minSalary FROM Employee) AS alias
) LIMIT 1;
希望这会有所帮助,
答案 1 :(得分:1)
尝试使用LIMIT和ORDER BY
DELETE FROM Employee
ORDER BY Salary
LIMIT 1;
如果有很多员工,那么您需要使用一个变量
SET @MinSalary = (SELECT MIN(Salary) FROM Employee);
DELETE FROM Employee
Where Salary = @MinSalary;
答案 2 :(得分:1)
您可以使用JOIN
:
delete e
from Employee e join
(select min(salary) as min_salary from employee) ee
on e.salary = ee.min_salary;
注意:我将participants
更改为employee
。考虑到您的描述和所得到的错误,这更有意义。
这是MySQL中的限制。您的代码可以在其他数据库中使用。