有两种方式,一种通用型:
SELECT a.node
FROM tree a
WHERE NOT existsselect (SELECT 1 FROM tree b WHERE a.node = b.parent)
START WITH PARENT = -1 / / 一棵树的根节点
CONNECT BY PARENT = PRIOR node;
一种使用于oracle 10g以上版本:
SELECT node
FROM (SELECT t.node, connect_by_isleaf lf
FROM tree t
START WITH t.parent = -1
CONNECT BY PRIOR t.node = t.parent)
WHERE lf = 1
node 树节点ID
parent 树节点的父节点