我需要加入一个表和一个子查询

时间:2011-11-15 23:12:31

标签: sql

SELECT * FROM Employee
(
   SELECT eid  FROM Assignment
   GROUP BY eid
   HAVING SUM(hours_per_week) > 40
)

这是我的代码,用于查找超过40的hours_per_week。子查询返回超过40小时的人的eid。我的问题是如何使用从子查询中获得的eid显示Employee中的所有人员。在哪里?还是加入?

3 个答案:

答案 0 :(得分:6)

使用WHERE ... IN (SELECT ...)

SELECT col1, col2, ..., coln
FROM Employee
WHERE eid IN
(
   SELECT eid  FROM Assignment
   GROUP BY eid
   HAVING SUM(hours_per_week) > 40
)

答案 1 :(得分:2)

SELECT e.*
FROM Employee AS e
INNER JOIN
(
   SELECT eid
   FROM Assignment
   GROUP BY eid
   HAVING SUM(hours_per_week) > 40
) AS ot ON ot.eid = e.eid

答案 2 :(得分:0)

Select E.EID, sum(A.hours_per_Week)
FROM employee E 
INNER join Assignment A on E.EID = A.EID
Group By E.EID 
Having sum(A.Hours_per_week) > 40

为什么需要子查询?