sys_connect_by_path函数用法

本文介绍 Oracle SQL 中的 sys_connect_by_path 函数,并通过示例演示如何使用该函数来构建层级结构,同时展示了如何判断叶子节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

sys_connect_by_path函数是用来返回组成层级的直到当前行的值。

例如:

 

select *
  from (select ltrim(sys_connect_by_path(id, ','), ',') id,
               ltrim(sys_connect_by_path(name, ','), ',') name,
               level as lvl,
               lpad(' ', 2 * (level - 1)) || id,
               lpad(' ', 2 * (level - 1)) || name,
               lead(level) over(order by id) leadlvlorder,
               case
                 when (level - lead(level) over(order by id)) < 0 then
                  0
                 else
                  1
               end isleaf
          from table_name c
         start with id = 0
        connect by prior c.id = c.parent_id)
 where isleaf = 1;

CONNECT_BY_ISLEAF 也可以判断叶子节点,如下:

select *
  from (select ltrim(sys_connect_by_path(id, ','), ',') id,
               ltrim(sys_connect_by_path(name, ','), ',') name,
               level as lvl,
               lpad(' ', 2 * (level - 1)) || id,
               lpad(' ', 2 * (level - 1)) || name,
               CONNECT_BY_ISLEAF isLeaf
          from table_name c
         start with id = 0
        connect by prior c.id = c.parent_id)
 where isleaf = 1;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值