我有以下查询在oracle的EMP表中查找不是经理的员工
select * from emp e1
where not exists (select null from emp e2
where e2.mgr=e1.empno)
我需要使用 start with connect by 子句进行输出,从而避免自连接
我有以下查询在oracle的EMP表中查找不是经理的员工
select * from emp e1
where not exists (select null from emp e2
where e2.mgr=e1.empno)
我需要使用 start with connect by 子句进行输出,从而避免自连接
有一个函数 CONNECT_BY_ISLEAF(),它指示分层查询中的给定行是否为叶节点。在 EMP 表中,不是经理的员工将是叶节点。
因此,我们可以在嵌套分层查询中使用此函数来过滤非经理:
select empno, mgr, ename
from (
select empno, mgr, ename, CONNECT_BY_ISLEAF cbi
from emp
start with mgr is null
connect by prior empno = mgr
) where cbi = 1
/
Oracle 有几个用于查询层次结构的简洁函数。 了解更多。