平均工资最低的部门加薪10%

问题描述 投票:0回答:1

这个面试问题解决方案不起作用,因为我在子查询中有两列,如果我使用

LIMIT
代替
IN
子句后的
=
,我就不能使用
WHERE
。我在 MySQL 上。

UPDATE employees 
SET salary = salary + (0.10*salary) 
WHERE team = (
    SELECT team, AVG(salary) avg_sal
    FROM employee
    GROUP BY team ORDER BY avg_sal LIMIT 1)
sql mysql sql-update subquery inner-join
1个回答
1
投票

可以让子查询只返回团队而不是两列。可能这就是您要编写的查询:

update employees e
set e.salary = 1.1 * e.salary
where team = (select team from employees group by team order by avg(salary) limit 1) 

不幸的是,这会引发错误

您不能在 FROM 子句中指定要更新的目标表 'e'

这是 MySQL 的典型限制,不允许您重新打开

where
子句中正在更新的表。相反,你可以
join

update employees e
inner join (select team from employees group by team order by avg(salary) limit 1) e1 
    on e1.team = e.team
set e.salary = 1.1 * e.salary
© www.soinside.com 2019 - 2024. All rights reserved.