這個方法只是對Oracle10以上纔有效,對sqlserver、mysql沒有測試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;
一、connect_by_isleaf是不是子節點;
二、sys_connect_by_path(tb.package_id, '|') ==> |xxx|xxx|xxx
三、start with tb.level_parent like '%|0|-1' ==> 樹的遍歷以從什麼開始
四、connect by prior tb.level_child = tb.level_parent; ==>此層遍歷以後,再將child做爲parent,繼續往下遍歷sql
select t.*, t.rowid from tbgm_goods_category toracle
--遞歸查出指定ID的全部上級節點
SELECT LPAD('',2*(LEVEL - 1), '') || gc.category_id AS category_id,gc.trade_id,gc.parent_category_idsqlserver
FROM tbgm_goods_category gc測試
CONNECT BY PRIOR parent_category_id = category_id -- 找出全部DEPT_ID等於當前記錄PARENT_ID的記錄code
START WITH category_id = 216 -- 從部門4開始遞歸查詢。server
--遞歸查出指定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的記錄。ci
START WITH category_id = 214 -- 從部門2開始遞歸查詢。
element
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)nfrom tbgm_goods_category tstart with t.parent_category_id=259connect by prior t.category_id=t.parent_category_id--and prior child_type=/'special/'group by rownum-level