数据库 
首页 > 数据库 > 浏览文章

Oracle递归查询start with connect by prior的用法

(编辑:jimmy 日期: 2024/9/21 浏览:3 次 )

一、基本语法

connect by递归查询基本语法是:

select 1 from 表格 start with ... connect by prior id = pId

start with:表示以什么为根节点,不加限制可以写1=1,要以id为123的节点为根节点,就写为start with id =123

connect by:connect by是必须的,start with有些情况是可以省略的,或者直接start with 1=1不加限制

prior:prior关键字可以放在等号的前面,也可以放在等号的后面,表示的意义是不一样的,比如 prior id = pid,就表示pid就是这条记录的根节点了

二、业务场景

举个例子,写条SQL:

t_user(用户表),t_unit_info(单位表),意思是以单位编码为"15803"的单位为根节点,查找出其单位及其子单位的用户信息

 select us.user_code, us.user_name
 from t_user us
 where exists (select 1
   from t_unit_info uinfo where 
   us.unit_code = uinfo.unit_code
   start with uinfo.unit_code = '15803'
   connect by prior uinfo.unit_code = uinfo.para_unit_code)

然后将prior换一下位置,发现只能查出单位编码为"15803"对应的单位,不能查子级单位

 select us.user_code, us.user_name
 from t_user us
 where exists (select 1
   from t_unit_info uinfo where 
   us.unit_code = uinfo.unit_code
   start with uinfo.unit_code = '15803'
   connect by uinfo.unit_code = prior uinfo.para_unit_code)

oracle 递归查询start with connect by prior的用法和知识不仅仅这些,本博客只是简单记录一下我所遇到的

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:oracle中利用关键字rownum查询前20名员工信息及rownum用法
下一篇:Oracle中的SUM用法讲解
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap