有兩種方式,一種通用型:
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 樹節點的父節點