为了账号安全,请及时绑定邮箱和手机立即绑定

请教关于oracle connect by 和 start with 的用法

请教关于oracle connect by 和 start with 的用法

萧十郎 2022-04-08 08:04:46
求大侠说一下。connect by prior ,在实际的开发应用中什么时候会用到?
查看完整描述

2 回答

?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

给你看个例子你就明白了

SQL> SELECT * FROM DEP;

DEPID DEPNAME UPPERDEPID
----------- -------------------------------------------------------------------------------- -----------
0 General Deparment
1 Development 0
2 QA 0
3 Server Development 1
4 Client Development 1
5 TA 2
6 Porject QA 2

--
SQL> SELECT RPAD( ' ', 2*(LEVEL-1), '-' ) || DEPNAME "DEPNAME",
CONNECT_BY_ROOT DEPNAME "ROOT",
CONNECT_BY_ISLEAF "ISLEAF",
LEVEL ,
SYS_CONNECT_BY_PATH(DEPNAME, '/') "PATH"
FROM DEP
START WITH UPPERDEPID IS NULL
CONNECT BY PRIOR DEPID = UPPERDEPID;

DEPNAME ROOT ISLEAF LEVEL PATH
------------------------------ ------------------- ---------- ---------- --------------------------------------------------------------------------------
General Deparment General Deparment 0 1 /General Deparment
-Development General Deparment 0 2 /General Deparment/Development
---Server Development General Deparment 1 3 /General Deparment/Development/Server Development
---Client Development General Deparment 1 3 /General Deparment/Development/Client Development
-QA General Deparment 0 2 /General Deparment/QA
---TA General Deparment 1 3 /General Deparment/QA/TA
---Porject QA General Deparment 1 3 /General Deparment/QA/Porject QA

1. CONNECT_BY_ROOT 返回当前节点的最顶端节点
2. CONNECT_BY_ISLEAF 判断是否为叶子节点,如果这个节点下面有子节点,则不为叶子节点
3. LEVEL 伪列表示节点深度
4. SYS_CONNECT_BY_PATH函数显示详细路径,并用“/”分隔



查看完整回答
反对 回复 2022-04-11
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

递归查询,当数据结构呈现为树形、链表形时非常有效。
例如,菜单、组织机构等。

查看完整回答
反对 回复 2022-04-11
  • 2 回答
  • 0 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信