您的位置:首页技术文章
文章详情页

Oracle用Start with...Connect By子句递归查询

【字号: 日期:2023-11-21 13:47:15浏览:28作者:猪猪
Start with...Connect By子句递归查询一般用于一个表维护树形结构的应用。创建示例表:CREATE TABLE TBL_TEST( ID;NUMBER, NAME; VARCHAR2(100 BYTE), PIDNUMBER; 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_TESTstart with id=1connect by prior id = pid从末梢往树ROOT递归select * from TBL_TESTstart with id=5connect by prior pid = id
标签: Oracle 数据库