我是Oracle分层查询的新手。我有一张桌子,有下面的数据。
Table Data Result Data 我的问题是为什么父母id(100)不包括在结果中? 下面是查询。
select id, lpad(' ',4*(LEVEL - 1)) || CHILD CHILD, LEVEL
from temp
START WITH PARENT = 100
CONNECT BY PARENT = PRIOR CHILD;
关于, hu山
答案 0 :(得分:1)
如果要在输出中包括起始值,请使用并集所有:
select 0 id, '100' child, 0 lvl from dual
union all
select id, lpad(' ', 4 * level ) || child, level
from temp
start with parent = 100
connect by parent = prior child
或递归CTE:
with c(id, child, lvl) as (
select 0, '100', 0 from dual
union all
select t.id, lpad(t.child, (c.lvl + 2) * 4, ' '), c.lvl + 1
from c join temp t on t.parent = c.child)
search depth first by id set seq
select id, child, lvl from c;
或首先将其添加到源数据:
select id, lpad(' ', 4 * (level-1) ) || child child, level
from (select id, parent, child from temp union all
select null, null, 100 from dual )
start with child = 100
connect by parent = prior child