加薪的SQL查询

时间:2010-08-20 11:09:47

标签: sql

我提出了以下查询,这个问题没有那么难以解决,但我认为可能有更好的方法。任何建议?

我有下表:

tb_Salary

EmpID DeptID Salary
---------------------
1  1  20000
2  1  20000
3  2  30000
4  2  800
5  2  200

我想用卢比增加员工的工资。 1000只当部门的平均工资超过35000时。

这可以使用单一更新查询吗?

我是按照以下方式做到的。但似乎并不是那种聪明的解决方案。

UPDATE  tb_Salary t1
SET  t1.Salary=t1.Salary+1000
WHERE  35000 < (select AVG(t2.Salary) from tb_Salary t2 WHERE t2.DeptID=t1.DeptID)

3 个答案:

答案 0 :(得分:3)

您的查询是可行的方法。

答案 1 :(得分:3)

UPDATE  tb_salary
    SET tb_salary.salary = tb_salary + 1000
FROM    tb_salary
        INNER JOIN
        (SELECT   avg(salary) AS avg_salary,
                  deptID
         FROM     tb_salary
         GROUP BY tb_salary.deptID
         HAVING   avg(salary) > 10000) AS salary_increase
        ON salary_increase.deptID = tb_salary.deptID;    

我没有时间对此进行测试,因此可能无法正常工作。 通常我会避免多个“Where”条件,因为它无效。

答案 2 :(得分:0)

update employee 
  set salary=salary+1000
  where deptid in (select deptid from employee group by deptid having sum(salary)>35000);