0

我目前有以下 SQL 查询,其中列出了在雇用分析师的部门工作的所有员工的所有姓名。

SELECT ename
FROM emp
WHERE deptno IN (SELECT DISTINCT emp.deptno
                 FROM emp, dept
                 WHERE emp.deptno = dept.dept
                 AND job = 'Analyst');

如何修改它以显示在雇用两名或两名以上分析师的部门工作的所有员工的姓名?

4

2 回答 2

2

您可以使用GROUP BY获取每个部门的分析师计数,然后根据此计数过滤员工:

SELECT emp.ename
FROM 
emp, (
    SELECT emp.deptno, count(*) as analyst_count
        FROM emp, dept
        WHERE emp.deptno = dept.dept
        AND job = 'Analyst'
        GROUP BY emp.deptno
) foo
WHERE emp.deptno = foo.deptno
AND foo.analyst_count >= 2;
于 2012-06-18T10:02:38.390 回答
1

GROUP BY使用and修改子查询HAVING

SELECT emp.deptno
FROM emp, dept
WHERE emp.deptno = dept.dept AND job = 'Analyst'
GROUP BY emp.deptno HAVING COUNT(*) > 1
于 2012-06-18T10:10:59.033 回答