结果未出现根级节点

Root level node is not coming in result

我是 Oracle 分层查询的新手。我有一个 table 并且有以下数据。

Table Data Result Data 我的问题是为什么 parent id(100) 不包含在结果中? 下面是查询。

select id, lpad(' ',4*(LEVEL - 1)) || CHILD CHILD, LEVEL
from temp
START WITH PARENT = 100
CONNECT BY  PARENT = PRIOR CHILD;

此致, 布山

如果要在输出中包含起始值,则使用 union all:

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

demo with all queries