這個方法只是對oracle10以上才有效,對sqlserver、mysql沒有測試
select connect_by_isleaf isleaf,
tb.switch_type_id,
sys_connect_by_path(tb.package_id, '|') path,
tb.record_type,
tb.element_offset,
tb.element_length,
tb.element_encode_type,
tb.element_align,
tb.mask,
tb.asn_type
from (select b.switch_type_id,
b.package_id,
b.element_offset,
b.element_length,
b.element_encode_type,
nvl(b.element_align, 'l') element_align,
b.record_type,
nvl(b.mask, 'lh') mask,
b.asn_type,
b.switch_type_id || '|' || b.package_level || '|' || b.package_id level_child,
b.switch_type_id || '|' || (b.package_level - 1) || '|' || b.parent_package_id level_parent
from switch_asn_record b
) tb
start with tb.level_parent like '%|0|-1'
connect by prior tb.level_child = tb.level_parent;
1、connect_by_isleaf是否是子節點;
2、sys_connect_by_path(tb.package_id, '|') ==> |***|***|***
3、start with tb.level_parent like '%|0|-1' ==> 樹的遍歷以從什麼開始
4、connect by prior tb.level_child = tb.level_parent; ==>此層遍歷之後,再將child作為parent,繼續往下遍歷
select t.*, t.rowid from tbgm_goods_category t
--遞迴查出指定id的所有上級節點
select lpad('',2*(level - 1), '') || gc.category_id as category_id,gc.trade_id,gc.parent_category_id
from tbgm_goods_category gc
connect by prior parent_category_id = category_id -- 找出所有dept_id等於當前記錄parent_id的記錄
start with category_id = 216 -- 從部門4開始遞迴查詢。
--遞迴查出指定id的所有子級節點
select lpad('',2*(level - 1), '')||gc.category_id as category_id,gc.trade_id,gc.parent_category_id,gc.category_depth
from tbgm_goods_category gc
where category_depth <= (select category_depth from tbgm_goods_category where category_id = 214)+1
connect by parent_category_id = prior category_id -- 找出所有parent_id等於當前記錄dept_id的記錄。
start with category_id = 214 -- 從部門2開始遞迴查詢。
oracle 9i中查詢乙個節點的所有制葉子節點
select
max(t.parent_category_id)keep(dense_rank last order by level)p,
max(t.category_id)keep(dense_rank last order by level)c,
max(t.category_name)keep(dense_rank last order by level)n
from tbgm_goods_category t
start with t.parent_category_id=259
connect by prior t.category_id=t.parent_category_id
--and prior child_type=/'special/'
group by rownum-level
Oracle表中實現遞迴查詢樹形結構
若將乙個樹狀結構儲存在一張表裡,需要在表中存入兩個欄位id和parentid,表示每一條記錄的parent是誰。table treetable idparentid 欄位3 1nulla 21b 32d oracle中可以使用 select from start with 開始的節點 connect...
ORACLE樹形結構查詢
在oracle資料庫查詢中,我們經常會遇到對樹型結構表的查詢,這是個麻煩的問題。下面給大家介紹一種sql語句,實現遞迴查詢。語法如下 select 欄位1,欄位2,欄位3,from 表名 start with 條件1 connect by prior 條件2 where 條件3 下面舉乙個例子,有這...
Oracle查詢樹形結構
oracle中的select語句可以用start with.connect by prior子句實現遞迴查詢,connect by 是結構化查詢中用到的,其基本語法是 select from tablename start with cond1 connect by cond2 where cond...