开发中我们难免会遇到一类问题是维护的数据存在父子级的关系,比如树状结构的数据,省市区镇的行政区划数据,而做查询操作时往往会遇到递归查询的场景,下面便讲解在Oracle数据库中如何进行递归查询。
Oracle递归查询语法
select <column> from <table> start with 条件1 connect by prior 条件2 where 条件3;
- 条件1是根结点的限定语句,在自顶向下查询树结构时,不但可以从根节点开始,还可以定义任何节点为起始节点,以此开始向下查找。这样查找的结果就是以该节点为开始的结构树的一枝。当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。比如start with id = 2表示从id=2的节点开始查询。
- 条件2是连接条件,由此条件确定父子节点之间的关系,比如条件是id=parent_id,那么父子关系的确立就是通过id和parent_id两个字段来完成的,**另外非常重要的一点是PRIOR关键字的位置,他决定了检索顺序是自顶向下还是自底向上。**连接条件中被PRIOR修饰的字段是当前记录的字段,未被其修饰的字段是下一条记录的字段。
1、PRIOR id=parent_id,这种情况下id是当前记录,下一条记录是满足条件的父parent_id,这就是自底向上的检索;
2、id= PRIOR parent_id,这种情况下parent_id是当前记录,子id是下一条记录,这就是自顶向下的检索,等同于PRIOR parent_id=id的写法
-条件3是过滤条件,用于对查到的所有记录进行过滤
示例
--菜单目录结构表
create table tb_menu(
id number(10) not null, --主键id
title varchar2(50), --标题
parent number(10) --parent id
)
--父菜单
insert into tb_menu(id, title, parent) values(1, '父菜单1',null);
insert into tb_menu(id, title, parent) values(2, '父菜单2',null);
insert into tb_menu(id, title, parent) values(3, '父菜单3',null);
insert into tb_menu(id, title, parent) values(4, '父菜单4',null);
insert into tb_menu(id, title, parent) values(5, '父菜单5',null);
--一级菜单
insert into tb_menu(id, title, parent) values(6, '一级菜单6',1);
insert into tb_menu(id, title, parent) values(7, '一级菜单7',1);
insert into tb_menu(id, title, parent) values(8, '一级菜单8',1);
insert into tb_menu(id, title, parent) values(9, '一级菜单9',2);
insert into tb_menu(id, title, parent) values(10, '一级菜单10',2);
insert into tb_menu(id, title, parent) values(11, '一级菜单11',2);
insert into tb_menu(id, title, parent) values(12, '一级菜单12',3);
insert into tb_menu(id, title, parent) values(13, '一级菜单13',3);
insert into tb_menu(id, title, parent) values(14, '一级菜单14',3);
insert into tb_menu(id, title, parent) values(15, '一级菜单15',4);
insert into tb_menu(id, title, parent) values(16, '一级菜单16',4);
insert into tb_menu(id, title, parent) values(17, '一级菜单17',4);
insert into tb_menu(id, title, parent) values(18, '一级菜单18',5);
insert into tb_menu(id, title, parent) values(19, '一级菜单19',5);
insert into tb_menu(id, title, parent) values(20, '一级菜单20',5);
--二级菜单
insert into tb_menu(id, title, parent) values(21, '二级菜单21',6);
insert into tb_menu(id, title, parent) values(22, '二级菜单22',6);
insert into tb_menu(id, title, parent) values(23, '二级菜单23',7);
insert into tb_menu(id, title, parent) values(24, '二级菜单24',7);
insert into tb_menu(id, title, parent) values(25, '二级菜单25',8);
insert into tb_menu(id, title, parent) values(26, '二级菜单26',9);
insert into tb_menu(id, title, parent) values(27, '二级菜单27',10);
insert into tb_menu(id, title, parent) values(28, '二级菜单28',11);
insert into tb_menu(id, title, parent) values(29, '二级菜单29',12);
insert into tb_menu(id, title, parent) values(30, '二级菜单30',13);
insert into tb_menu(id, title, parent) values(31, '二级菜单31',14);
insert into tb_menu(id, title, parent) values(32, '二级菜单32',15);
insert into tb_menu(id, title, parent) values(33, '二级菜单33',16);
insert into tb_menu(id, title, parent) values(34, '二级菜单34',17);
insert into tb_menu(id, title, parent) values(35, '二级菜单35',18);
insert into tb_menu(id, title, parent) values(36, '二级菜单36',19);
insert into tb_menu(id, title, parent) values(37, '二级菜单37',20);
--三级菜单
insert into tb_menu(id, title, parent) values(38, '三级菜单38',21);
insert into tb_menu(id, title, parent) values(39, '三级菜单39',22);
insert into tb_menu(id, title, parent) values(40, '三级菜单40',23);
insert into tb_menu(id, title, parent) values(41, '三级菜单41',24);
insert into tb_menu(id, title, parent) values(42, '三级菜单42',25);
insert into tb_menu(id, title, parent) values(43, '三级菜单43',26);
insert into tb_menu(id, title, parent) values(44, '三级菜单44',27);
insert into tb_menu(id, title, parent) values(45, '三级菜单45',28);
insert into tb_menu(id, title, parent) values(46, '三级菜单46',28);
insert into tb_menu(id, title, parent) values(47, '三级菜单47',29);
insert into tb_menu(id, title, parent) values(48, '三级菜单48',30);
insert into tb_menu(id, title, parent) values(49, '三级菜单49',31);
insert into tb_menu(id, title, parent) values(50, '三级菜单50',31);
commit;
- 查询一节点下所有直属子节点,包括子节点,子节点的子节点
select * from tb_menu m start with m.id=1 connect by m.parent= prior m.id;
- 查询一节点下所有直属父节点,包括父节点,父节点的父节点
select * from tb_menu m start with m.id=38 connect by prior m.parent=m.id;
- 判断节点是否为叶子节点
select connect_by_isleaf, tb_menu.* from tb_menu
start with parent is null
connect by parent = prior id;
connect_by_isleaf函数用来判断当前节点是否包含下级节点,如果包含的话,说明不是叶子节点,这里返回0;反之,如果不包含下级节点,这里返回1。