如何联接具有多个约束的四个表

时间:2019-01-24 04:33:21

标签: sql ms-access inner-join

我必须编写一条SQL语句来显示相似类型的总和,我还需要从另一个表中添加另一个排除约束。

这正在使用Microsoft Access 2010。 问题显示为:编写一条SQL语句以显示每种类型OWNER的总工作时间,但不包括体验级别为Junior的员工提供的服务,并排除少于三个成员的任何类型的员工。

 SELECT OWNER.Type, SUM(HoursWorked) AS Total_Work_Hours
FROM OWNER AS O, PROPERTY AS P, SERVICE AS S, EMPLOYEE AS E
WHERE O.OwnerID = P.OwnerID
AND P.Property= S.PropertyID
AND S.EmployeeID=E.EmployeeID
AND O.Type>3;

2 个答案:

答案 0 :(得分:1)

您可以尝试一下。

SELECT O.[Type], SUM(e.HoursWorked) AS Total_Work_Hours,count(e.employeeid) as Total_Members
FROM OWNER AS O, PROPERTY AS P, SERVICE AS S, EMPLOYEE AS E
WHERE O.OwnerID = P.OwnerID
AND P.Property= S.PropertyID
AND S.EmployeeID=E.EmployeeID
AND e.ExperienceLevel NOT IN ('Junior')
Group by o.[Type]
Having count(e.employeeid)>=3;

答案 1 :(得分:0)

学习使用正确的,明确的,标准 GROUP BY语法。您还需要适当的SELECT o.Type, SUM(HoursWorked) AS Total_Work_Hours FROM ((OWNER AS O INNER JOIN PROPERTY AS P ON O.OwnerID = P.OwnerID ) INNER JOIN SERVICE AS S ON P.Property = S.PropertyID ) INNER JOIN EMPLOYEE AS E ON S.EmployeeID = E.EmployeeID WHERE O.Type > 3 AND E.ExperienceLevel <> 'Junior' GROUP BY o.Type HAVING COUNT(*) >= 3; ,并且应该对所有列引用进行限定:

{{1}}