我目前有一个查询,以查找一名员工是否正在处理多项任务,并返回员工的姓名。
SELECT Eid, Count(Eid),
(SELECT Ename from Employee where Employee.Eid = Assignment.Eid) as EName
FROM Assignment
GROUP BY Eid HAVING Count(Eid) > 1;
员工表有EID和EName字段。分配有EID和AssignmentNum。
目前它的工作原理,但我认为使用Inner Join可以更好地编写,但我似乎无法使用有效的连接编写查询。任何帮助将不胜感激。
答案 0 :(得分:1)
你会这样做:
SELECT a.Eid, Count(*), e.EName
FROM Assignment a JOIN
Employee e
ON e.Eid = a.Eid
GROUP BY a.Eid, e.name
HAVING Count(*) > 1;
答案 1 :(得分:1)
SELECT e.ename, a.Eid, Count(a.Eid),
from Employee
Inner join assignment as a on e.Eid = a.Eid
GROUP BY a.Eid
HAVING Count(a.Eid) > 1;
您在子查询中拥有必要的连接逻辑。