日期:2014-05-17  浏览次数:20885 次

oracle查询树节点问题
表结构及测试数据如下:
create table TEST_TREE
(
  id VARCHAR2(32),
  name VARCHAR2(32),
  pid VARCHAR2(32),
  type VARCHAR2(32),
  lev VARCHAR2(32)
);
comment on column TEST_TREE.name
  is '名称';
comment on column TEST_TREE.pid
  is '父ID';
comment on column TEST_TREE.type
  is '种类';
comment on column TEST_TREE.lev
  is '级别';
insert into TEST_TREE (id, name, pid, type, lev)
values ('0001', '中国', null, null, '0');
insert into TEST_TREE (id, name, pid, type, lev)
values ('000101', '北京', '0001', '1', '1');
insert into TEST_TREE (id, name, pid, type, lev)
values ('000102', '湖南省', '0001', '2', '1');
insert into TEST_TREE (id, name, pid, type, lev)
values ('000103', '河南省', '0001', '2', '1');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010101', '东城区', '000101', '1', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010102', '西城区', '000101', '2', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010103', '崇文区', '000101', '4', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010201', '长沙市', '000102', '1', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010202', '岳阳市', '000102', '3', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010203', '怀化市', '000102', '1', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010301', '郑州市', '000103', '1', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010302', '开封市', '000103', '2', '2');
insert into TEST_TREE (id, name, pid, type, lev)
values ('00010303', '洛阳市', '000103', '4', '2');

问题如下:
要求根据id和type,查询当前节点下所有子节点的type为4的完整行政区划,完整行政区划要从当前查询的节点开始,以下为2种查询的效果
(1)查询条件:id='0001',type='4'
查询后的结果如下:
0001 中国  
000101 北京 0001 1
00010103 崇文区 000101 4
000103 河南省 0001 2
00010303 洛阳市 000103 4
(2)查询条件:id='000103',type='4'
查询后的结果如下:
000103 河南省 0001 2
00010303 洛阳市 000103 4

------解决方案--------------------

(1)
select id,name,nvl(pid,'') as pid, nvl(type,'')as type, nvl(lev,'') as lev from TEST_TREE where id like'0001%'
或者
select id,name,nvl(pid,'') as pid, nvl(type,'')as type, nvl(lev,'') as lev from TEST_TREE where left(id,4)='0001'
(2)
select id,name,nvl(pid,'') as pid, nvl(type,'')as type, nvl(lev,'') as lev from TEST_TREE where id like'000103%'
或者
select id,name,nvl(pid,'') as pid, nvl(type,'')as type, nvl(lev,'') as lev from TEST_TREE where left(id,4)='000103'

------解决方案--------------------
select distinct t2.id,t2.name,t2.pid,t2.type,t2.lev from (select * from TEST_TREE t1 where t1.pid like '0001%' or t1.id = '0001') t
left join test_tree t2 on t.pid like t2.id||'%' 
and t.type = 4
and t2.id is not null
union all 
select tt.id,tt.name,tt.pid,tt.type,tt.lev from test_tree tt where tt.type = 4 and pid like '0001%'
order by id asc
------解决方案--------------------
select * from TEST_TREE t
start with t.id=0001 --and t.type='1'
connect by prior t.id=t.pid;


但是感觉 type 字段不合适
------解决方案--------------------
这个问题不难,有两种解决方法:
1、先找出type=4的所有父节点,再在这个结果集中找出节点为id='0001'的所有子节点,即可。
2、先找出节点为id='0001'的所有子节点,再在这个结果集中找出节点type=4的所有父节点,最后的这个结果集即是。

这两种方法本质上一样。要么从父找到子,要么从子找到父。
其实这是个追溯问题。
SQL code
--从父亲找子
select t.*,level   
from t
start with t.pid='0001' --定位父节点
connect by prior t.id=t.pid; --追溯子节点

--从子找父
select t.*,level 
from t
start with t.type=4 --定位子节点
conn