在更新查询中使用多个左联接

时间:2018-07-09 09:40:33

标签: sql sql-server

我使用了一个选择查询<img [src]="photo.url">

having count(JobDegree)>1

如下所示:

Employees left join JobDegree on Employees.Id = JobDegree.employeeId

我想更新每位员工的薪水,其中select Employees.Id, count(JobDegree.DegreeId) as 'Degrees',Contracts.Salary from Employees left join JobDegree on Employees.Id = JobDegree.employeeId left join Contracts on Employees.Id = Contracts.EmployeeId group by Employees.Id,Contracts.Salary having COUNT(JobDegree.DegreeId) > 1  以count(JobDegree.Id)>1为单位,但员工的薪水位于另一个名为Contracts set Salary = Salary + 100的表中 和“合同”包含我要加入的列Contracts.Salary Contracts.employeeId更新查询使我感到困惑,将不胜感激。

4 个答案:

答案 0 :(得分:1)

我想这就是你想要的

Update Contracts
set Salary = Salary + 100
where Contracts.employeeId in
(select Employees.Id
from Employees
left join JobDegree on Employees.Id = JobDegree.employeeId
left join Contracts on Employees.Id = Contracts.EmployeeId
group by Employees.Id,Contracts.Salary
having COUNT(JobDegree.DegreeId) > 1)

答案 1 :(得分:1)

我认为您可以这样做:

update c 
    set salary = c.salary + 100
from contracts c inner join 
     (select jd.employeeId
      from JobDegree jd
      group by jd.employeeId
      having count(*) > 1
     ) jd
     on jd.EmployeeId = c.EmployeeId

请注意,Employees表本身是不需要的。

答案 2 :(得分:0)

您可以将subqueryupdate语句一起使用:

update c 
    set c.salary = c.salary + 100
from contracts c inner join 
     (select e.id
      from Employees e left join
           JobDegree jd
           on jd.employeeId = e.id
      group by e.id
      having count(jd.DegreeId) > 1
     ) s
     on s.id = c.EmployeeId;

答案 3 :(得分:0)

这是我尝试并提供了很多帮助的答案in

update Contracts set Salary= Salary + 100 where Contracts.EmployeeId in (
select Employees.Id
from Employees
left join JobDegree on Employees.Id = JobDegree.employeeId
group by Employees.Id
having COUNT(JobDegree.DegreeId) > 1)