这个方法只是对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, '|') ==> |xxx|xxx|xxx3、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_idFROM 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_depthFROM 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)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