答案 0 :(得分:3)
使用分层查询并使用pseudocolumns CONNECT_BY_ISLEAF
和CONNECT_BY_ISCYCLE
将输出限制为仅非循环叶子行:
Oracle安装程序:
CREATE TABLE your_table ( parent_column, child_column ) AS
SELECT 1, 2 FROM DUAL UNION ALL
SELECT 2, 3 FROM DUAL UNION ALL
SELECT 3, 4 FROM DUAL UNION ALL
SELECT 4, 5 FROM DUAL UNION ALL
SELECT 3, 6 FROM DUAL UNION ALL
SELECT 3, 7 FROM DUAL UNION ALL
SELECT 7, 8 FROM DUAL;
<强>查询强>:
SELECT child_column
FROM your_table
WHERE CONNECT_BY_ISLEAF = 1
AND CONNECT_BY_ISCYCLE = 0
START WITH parent_column = 1
CONNECT BY NOCYCLE PRIOR child_column = parent_column
<强>输出强>:
CHILD_COLUMN
------------
5
6
8