到目前为止,我有以下内容:
SELECT Emp.Empno as "Emp#", Emp.Ename as "Employee", Emp.Deptno as "Department", Emp.Mgr as "Mgr#", M.Ename as "Manager",
FROM Emp
LEFT OUTER JOIN Emp M ON Emp.Mgr = M.Empno;
结果:
Emp# Employee DEPTNO Mgr# Manager
---------- ---------- ---------- ---------- ----------
7902 FORD 20 7566 JONES
7788 SCOTT 20 7566 JONES
7900 JAMES 30 7698 BLAKE
7844 TURNER 30 7698 BLAKE
7654 MARTIN 30 7698 BLAKE
7521 WARD 30 7698 BLAKE
7499 ALLEN 30 7698 BLAKE
7934 MILLER 10 7782 CLARK
7876 ADAMS 20 7788 SCOTT
7782 CLARK 10 7839 KING
7698 BLAKE 30 7839 KING
7566 JONES 20 7839 KING
7369 SMITH 20 7902 FORD
7839 KING 10
但是,我还需要知道如何从我的结果中删除Employee的DEPTNO与其各自经理(如果有的话)的DEPTNO不匹配的行。
我真的很累,已经坚持了几个小时。
答案 0 :(得分:3)
你可以在你的sql中添加一个where子句,只选择经理和员工deptno是否相同
SELECT E.Empno as "Emp#", E.Ename as "Employee", E.Deptno as "Department", E.Mgr as "Mgr#", M.Ename as "Manager"
FROM Emp E
LEFT OUTER JOIN Emp M ON E.Mgr = M.Empno
WHERE E.Deptno = M.Deptno;