CREATE TABLE SC_DISTRICT ( ID NUMBER(10) NOT NULL, PARENT_ID NUMBER(10), NAME VARCHAR2(255 BYTE) NOT NULL ); ALTER TABLE SC_DISTRICT ADD ( CONSTRAINT SC_DISTRICT_PK PRIMARY KEY (ID)); ALTER TABLE SC_DISTRICT ADD ( CONSTRAINT SC_DISTRICT_R01 FOREIGN KEY (PARENT_ID) REFERENCES SC_DISTRICT (ID) );
insert into A_DISTRICT (id, parent_id, name) values (1, null, '河南省'); insert into A_DISTRICT (id, parent_id, name) values (2, null, '山东省'); insert into A_DISTRICT (id, parent_id, name) values (3, 1, '郑州市'); insert into A_DISTRICT (id, parent_id, name) values (4, 1, '开封市'); insert into A_DISTRICT (id, parent_id, name) values (5, 1, '商丘市'); insert into A_DISTRICT (id, parent_id, name) values (6, 3, '中原区'); insert into A_DISTRICT (id, parent_id, name) values (7, 3, '金水区'); insert into A_DISTRICT (id, parent_id, name) values (8, 3, '二七区'); insert into A_DISTRICT (id, parent_id, name) values (9, 3, '城管回族区'); insert into A_DISTRICT (id, parent_id, name) values (10, 3, '上街区'); insert into A_DISTRICT (id, parent_id, name) values (11, 3, '惠济区'); insert into A_DISTRICT (id, parent_id, name) values (12, 4, '开封县'); insert into A_DISTRICT (id, parent_id, name) values (13, 4, '杞县'); insert into A_DISTRICT (id, parent_id, name) values (14, 4, '兰考县'); insert into A_DISTRICT (id, parent_id, name) values (15, 4, '通许县'); insert into A_DISTRICT (id, parent_id, name) values (16, 13, '城关镇'); insert into A_DISTRICT (id, parent_id, name) values (17, 13, '五里河镇'); insert into A_DISTRICT (id, parent_id, name) values (18, 13, '邢口镇'); insert into A_DISTRICT (id, parent_id, name) values (19, 13, '柿园乡'); insert into A_DISTRICT (id, parent_id, name) values (20, 13, '城郊乡');生成数据如下
递归语法和主要使用函数
select * from 表 start with 条件入口 connect by prior id = parent_id(向下)
select * from 表 start with 条件入口 connect by prior parent_id = id(向上)
如果不写prior,表明前序遍历 未指明遍历方向,所以不进行递归,习惯上会在语句后面加上order by排序和group by分组
connect_by_root(列名) 查询根节点
connect_by_isleaf 查询是否叶子节点,0是,1不是
sys_connect_by_path 查询递归路径
select id, parent_id, name from a_district start with parent_id is null connect by id = parent_id order by id
select id, parent_id, name from a_district start with name='杞县' connect by prior parent_id = id order by id
select id, parent_id, name,connect_by_root(id) city_id, connect_by_root(name) city_name from a_district start with name='杞县' connect by prior parent_id = id order by id
select id, parent_id, name from a_district start with name='杞县' connect by prior id = parent_id order by id
select id, parent_id, name, connect_by_root(id) city_id, connect_by_root(name) city_name from a_district start with name='开封市' connect by prior id = parent_id order by id
select id, name, parent_id, level, connect_by_isleaf isleaf from a_district start with name = '城郊乡' connect by prior parent_id = id order by id
select id, name, parent_id, level, connect_by_isleaf isleaf from a_district start with name = '河南省' connect by prior id = parent_id order by id
select id, name, parent_id, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='杞县' connect by prior parent_id = id order by id
select id, name, parent_id, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='河南省' connect by prior id = parent_id order by id
select id, name, parent_id, level, connect_by_isleaf isleaf, substr(sys_connect_by_path(name,'->'),3) name_path from a_district start with name='河南省' connect by prior id = parent_id order by id