TA的每日心情 | 开心 2015-10-14 21:28 |
---|
签到天数: 31 天 连续签到: 1 天 [LV.5]常住居民I
|
东陆风华帐号全站通用,包括论坛、商城、网络家园等站点,登陆查看更精彩内容
您需要 登录 才可以下载或查看,没有帐号?注册东陆风华通行证
x
Start with...Connect By子句递归查询一般用于一个表维护树形结构的应用。
创建示例表:
CREATE TABLE TBL_TEST
(
ID NUMBER,
NAME VARCHAR2(100 BYTE),
PID NUMBER DEFAULT 0
);
插入测试数据:
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');
从Root往树末梢递归
select * from TBL_TEST
start with id=1
connect by prior id = pid
从末梢往树ROOT递归
select * from TBL_TEST
start with id=5
connect by prior pid = id
select * from test_tt;
1 10 0
2 11 1
3 20 0
4 12 1
5 121 2
select * from test_tt
start with id=1
connect by prior id = pid
1 10 0
2 11 1
5 121 2
4 12 1
select * from TBL_TEST
start with id=5
connect by prior pid = id
5 121 2
2 11 1
1 10 0
select * from test_tt
start with id=5
connect by prior id = pid
5 121 2 |
|