Oracle中自带的递归查询(connect by)
做个测试
select * from a
start with id=1
connect by prior id = pid;
id name pid
1 10 0
2 11 1
5 121 2
4 12 1
从末梢往树ROOT递归
select * from a
start with id=5
connect by prior pid = id
id name pid
1 10 0
2 11 1
5 121 2
4 12 1
CREATE TABLE a
(
ID NUMBER,
NAME VARCHAR2(100 BYTE),
PID NUMBER DEFAULT 0
);
INSERT INTO a(ID,NAME,PID) VALUES('1','10','0');
INSERT INTO a(ID,NAME,PID) VALUES('2','11','1');
INSERT INTO a(ID,NAME,PID) VALUES('3','20','0');
INSERT INTO a(ID,NAME,PID) VALUES('4','12','1');
INSERT INTO a(ID,NAME,PID) VALUES('5','121','2');
从Root往树末梢递归
select * from a;
id name pid
1 10 0
2 11 1
3 20 0
4 12 1
5 121 2select * from a
start with id=1
connect by prior id = pid;
id name pid
1 10 0
2 11 1
5 121 2
4 12 1
从末梢往树ROOT递归
select * from a
start with id=5
connect by prior pid = id
id name pid
1 10 0
2 11 1
5 121 2
4 12 1
posted on 2011-11-17 16:08 java课程设计例子 阅读(212) 评论(0) 编辑 收藏 举报