获取树型节点的全名称

-- 获取树型节点的全名称:父节点名称 + ... + 当前节点名称
with test(fid, fname, fparentid) as (
-- 结果1, 见附图
values('01', 'test01', '0')
union
values('0101', 'test0101', '01')
union
values('010101', 'test010101', '0101')
union
values('0102', 'test0102', '01')
union
values('02', 'test02', '0')

),
temp(fid, fname, fparentid) as (
--由于连接后的名称比较长,所以需要增加varchar的长度
select fid, cast( fname as varchar(100) ), fparentid from test -- where 条件
union all
select child.fid, cast( parent.fname||'/'||child.fname as varchar(100) ), parent.fparentid from temp as child, test as parent where child.fparentid = parent.fid
)
-- 结果3, 见附图
select t.fid, t.fname, test.fparentid from (
-- 结果2(没有过滤fparentid = '0'时), 见附图
select fid, fname, fparentid from temp where fparentid = '0' order by fid
) as t, test where t.fid = test.fid
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值