Oracle使用遞迴獲得首級和末級的內容
阿新 • • 發佈:2019-02-03
create table t2(
root_id number,
id number,
name varchar(5),
description varchar(10)
);
insert into t2(root_id,id,name,description) values(0,1,'a','aaa');
insert into t2(root_id,id,name,description) values(1,2,'a1','aaa1');
insert into t2(root_id,id,name,description) values(1,3,'a2','aaa2');
insert into t2(root_id,id,name,description) values(0,4,'b','bbb');
insert into t2(root_id,id,name,description) values(4,5,'b1','bbb1');
insert into t2(root_id,id,name,description) values(4,6,'b2','bbb2');
insert into t2(root_id,id,name,description) values(6,7,'7b2','bbb2');
insert into t2(root_id,id,name,description) values(-1,-2,'-a','-aaa');
insert into t2(root_id,id,name,description) values(-2,-5,'-a','-aaa');
create table t3(id number);
insert into t3(id)values(7);
insert into t3(id)values(3);insert into t3(id)values(6);
select * from t2
select substr(sys_connect_by_path(id,':'),2,2),connect_by_isleaf,T2.* from t2
where connect_by_isleaf = 1
start with id in (select id from t2)
connect by prior root_id = id
root_id number,
id number,
name varchar(5),
description varchar(10)
);
insert into t2(root_id,id,name,description) values(0,1,'a','aaa');
insert into t2(root_id,id,name,description) values(1,2,'a1','aaa1');
insert into t2(root_id,id,name,description) values(1,3,'a2','aaa2');
insert into t2(root_id,id,name,description) values(0,4,'b','bbb');
insert into t2(root_id,id,name,description) values(4,5,'b1','bbb1');
insert into t2(root_id,id,name,description) values(4,6,'b2','bbb2');
insert into t2(root_id,id,name,description) values(6,7,'7b2','bbb2');
insert into t2(root_id,id,name,description) values(-1,-2,'-a','-aaa');
insert into t2(root_id,id,name,description) values(-2,-5,'-a','-aaa');
create table t3(id number);
insert into t3(id)values(7);
insert into t3(id)values(3);insert into t3(id)values(6);
select * from t2
select substr(sys_connect_by_path(id,':'),2,2),connect_by_isleaf,T2.* from t2
where connect_by_isleaf = 1
start with id in (select id from t2)
connect by prior root_id = id