mySQL排除计数列,如果在

时间:2013-10-16 01:27:32

标签: mysql join count subquery

我试图找到每种类型的所有者的工作小时数,不包括具有“Junior”的ExperienceLevel和少于3名成员的任何ExperienceLevel类型的员工的服务。我一直在努力解决这个问题,无法得到答案....

TABLES:

OWNER ( OwnerID, OwnerName, OwnerEmail, OwnerType )
PROPERTY ( PropertyID, PropertyName, Street, City, State, Zip, OwnerID )
EMPLOYEE ( EmployeeID, LastName, FirstName, CellPhone, ExperienceLevel )
SERVICE ( PropertyID, EmployeeID, ServiceDate, HoursWorked )

我的最新查询:

SELECT OwnerType, SUM(HoursWorked) AS SumOfHoursWorked
 FROM EMPLOYEE, SERVICE, OWNER
 WHERE EMPLOYEE.EmployeeID = SERVICE.EmployeeID 
 AND EMPLOYEE.ExperienceLevel <> 'Junior'
 AND (SELECT COUNT(ExperienceLevel) > 2)

1 个答案:

答案 0 :(得分:0)

需要在select语句中的所有表之间加入条件,并且您只限定员工和服务之间的联接。

除了

where employee.employeeid = service.employeeid 

添加

and service.propertyid = property.propertyId 
and property.ownerid = owner.ownerid  

作为Mihai的评论建议

and employee.experiencelevel in (select experiencelevel from employee having count(*) > 2)

最后,如果值被分组,总和将更好地工作。

group by ownertype;