该楼层疑似违规已被系统折叠隐藏此楼查看此楼
-------------------------------------
--
-- 海枫科技 递归查询
-------------------------------------
-------------------------------------
-- Create table
-------------------------------------
create table TREE
(
id number,
pid number,
name varchar2(32)
)
;
-- Create/Recreate primary, unique and foreign key constraints
alter table TREE
add constraint pk_TREE primary key (ID);
truncate table tree;
--测试数据
insert into tree(id,pid,name) values(0,null,'B');
insert into tree(id,pid,name) values(1,null,'A');
insert into tree(id,pid,name) values(2,1,'AA1');
insert into tree(id,pid,name) values(3,1,'AA2');
insert into tree(id,pid,name) values(4,1,'AA3');
insert into tree(id,pid,name) values(5,4,'AAA31');
insert into tree(id,pid,name) values(6,4,'AAA32');
insert into tree(id,pid,name) values(7,4,'AAA33');
insert into tree(id,pid,name) values(8,7,'AAAA331');
insert into tree(id,pid,name) values(9,7,'AAAA332');
commit;
--查询所有
select * from tree;
--向下遍历,查询ID为1下面的所有子数据
--包括ID=1
select * from tree start with id = 4 connect by prior id=pid;
--不包括ID=1
select * from tree start with pid = 1 connect by prior id=pid;
--向上遍历,查询ID为7下面的所有上级
select * from tree start with id = 7 connect by prior pid=id;
如果觉得《查询月份近五个月数据oracle 海枫科技 递归查询 树形结构查询 oracle数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!