這真的很難說什麼你真的想基於你在你提供的有關資料來實現的,但在我看來,要顯示每個子樹ID
(從ID = 1開始的主樹,讓它們以這種方式排序,然後從ID = 2開始的子樹等一直向下)。
下面是一個例子:
with Tb_Table (id, parent_id, name) as(
select 1, 1, 'Child_1' from dual union all
select 2, 1, 'Child_2' from dual union all
select 3, 3, 'Child_3' from dual
),
rec_data (id, parent_id, name, lv) as(
select id
, parent_id
, name
, 0 lv
from Tb_Table
where id = id
union all
select tt.id
, tt.parent_id
, tt.name
, lv + 1
from rec_data rt
join tb_table tt
on (rt.id = tt.parent_id)
)
search depth first by id set ord
cycle id set is_cycle to 'Y' default 'N'
select id
, parent_id
, concat(lpad(' ', lv*3,' '), name) name
from rec_data
結果:
Id Parent_Id Name
-----------------------
1 1 Child_1
1 1 Child_1
2 1 Child_2
2 1 Child_2
3 3 Child_3
3 3 Child_3
Demo
什麼是 「C」?你沒有定義它。另外,你可以給出一個示例表和預期的查詢結果嗎? –
對不起,意思是把c放在節點後面,仍然有問題。 – moger777
你到底想要做什麼?你能給我們一些樣本數據和預期的產出嗎? –