码迷,mamicode.com
首页 > 其他好文 > 详细

目录树构建及查询案例(二)

时间:2015-07-18 17:07:09      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:目录树   行政区划   乡村代码   案例   connect by   

create table TREE
(
  TREEID   VARCHAR2(20) not null,
  LABLE    VARCHAR2(50) not null,
  PARENTID VARCHAR2(20)
);
--创建主键
alter table TREE add constraint PK_TREE primary key (TREEID);
--创建表内外键
alter table TREE add constraint FK_TREE foreign key (PARENTID) references TREE (TREEID);

insert into TREE (TREEID, LABLE, PARENTID) values (‘140000‘, ‘山西省‘, null);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140100‘, ‘太原市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140101‘, ‘市辖区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140105‘, ‘小店区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140106‘, ‘迎泽区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140107‘, ‘杏花岭区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140108‘, ‘尖草坪区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140109‘, ‘万柏林区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140110‘, ‘晋源区‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121‘, ‘清徐县‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121100‘, ‘清源镇‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121101‘, ‘徐沟镇‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121102‘, ‘东于镇‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121103‘, ‘孟封镇‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200‘, ‘马峪乡‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200200‘, ‘李家楼村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200201‘, ‘仁义村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200202‘, ‘东马峪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200203‘, ‘西马峪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200204‘, ‘都沟村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200205‘, ‘东梁泉村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200206‘, ‘西梁泉村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200207‘, ‘刘家元村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200208‘, ‘梁泉岭村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200209‘, ‘圪垛儿村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200210‘, ‘西沟村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200211‘, ‘桃园村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200212‘, ‘阳圈峁村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200213‘, ‘枣坪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200214‘, ‘黄土坡村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200215‘, ‘杏旺村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200216‘, ‘东迎南风村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200217‘, ‘西迎南风村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200218‘, ‘后窑村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200219‘, ‘涧沟村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200220‘, ‘东石窖村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200221‘, ‘西石窖村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200222‘, ‘碾底村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200223‘, ‘水峪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200224‘, ‘东圪台头村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200225‘, ‘寺家坪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200226‘, ‘武家崖村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200227‘, ‘程家沟村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200228‘, ‘陈家坪村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200229‘, ‘麦地掌村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200231‘, ‘西圪台头村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121200232‘, ‘张家山村委会‘, ‘140121200‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121201‘, ‘柳杜乡‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121202‘, ‘西谷乡‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121203‘, ‘王答乡‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140121204‘, ‘集义乡‘, ‘140121‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140122‘, ‘阳曲县‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140123‘, ‘娄烦县‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140181‘, ‘古交市‘, ‘140100‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140200‘, ‘大同市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140300‘, ‘阳泉市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140400‘, ‘长治市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140500‘, ‘晋城市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140600‘, ‘朔州市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140700‘, ‘晋中市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140800‘, ‘运城市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘140900‘, ‘忻州市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘141000‘, ‘临汾市‘, ‘140000‘);
insert into TREE (TREEID, LABLE, PARENTID) values (‘141100‘, ‘吕梁市‘, ‘140000‘);
commit;

--山西省下属市
select * from tree 
where treeid like ‘____00‘        
  CONNECT BY PRIOR treeid = parentid
         START WITH treeid = ‘140000‘
       ORDER BY treeid;
--太原市下属区县
select * from tree 
where treeid like ‘1401__‘        
  CONNECT BY PRIOR treeid = parentid
         START WITH treeid = ‘140100‘
       ORDER BY treeid;

--清徐县下属乡镇及村
select * from tree         
  CONNECT BY PRIOR treeid = parentid
         START WITH treeid = ‘140121‘
       ORDER BY treeid;
      
--马峪乡下属村
select * from tree      
  CONNECT BY PRIOR treeid = parentid
         START WITH treeid = ‘140121200‘
       ORDER BY treeid; 
--山西省所有的村
select * from tree      
where lengthb(treeid)>=12
  CONNECT BY PRIOR treeid = parentid
         START WITH treeid = ‘140000‘
       ORDER BY treeid; 
           

版权声明:本文为博主原创文章,未经博主允许不得转载。

目录树构建及查询案例(二)

标签:目录树   行政区划   乡村代码   案例   connect by   

原文地址:http://blog.csdn.net/sxdtzhp/article/details/46943171

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!