递归的使用:oracle&pgsql

一、数据准备

create table test(
id varchar2(2),
parent varchar2(10),
child varchar2(10)
);

insert into test  (id,parent,child)
values('1','a','ab');
insert into test  (id,parent,child)
values('2','a','ac');
insert into test  (id,parent,child)
values('3','b','bc');
insert into test  (id,parent,child)
values('4','ab','abc');
insert into test  (id,parent,child)
values('5','abc','abcd');
insert into test  (id,parent,child)
values('6','ac','acd');
insert into test  (id,parent,child)
values('7','bc','bcd');

查询数据结果:select * from test

1    a         ab
2    a         ac
3    b         bc
4    ab      abc
5    abc    abcd
6    ac      acd
7    bc      bcd
数据分析:存在两条递归

1 : a -> ab -> abc -> abcd  

     a -> ac-> acd

2 : b -> bc-> bcd

二、oracle数据库

关键字分析,详情见具体sql和查询结果:

       level:可以得到递归的第几层;

       start with :开始递归起点;

       connect by : 连接条件;

      prior : 优先的意思,个人理解:固定数据,在那个条件前面,该数据就固定了。

--最常用,已知父级查询所有子级
select t.parent,t.child,level from test t 
start with t.parent = 'a'
connect by prior t.child = t.parent;
a	ab	1
ab	abc	2
abc	abcd	3
a	ac	1
ac	acd	2

select t.parent,t.child,level from test t 
start with t.parent = 'b'
connect by prior t.child = t.parent;
b	bc	1
bc	bcd	2

select t.parent,t.child,level from test t 
start with t.parent = 'a'
connect by  t.child = prior t.parent;
a	ab	1
a	ac	1

select t.parent,t.child,level from test t 
start with t.parent = 'b'
connect by  t.child = prior t.parent;
b	bc	1
-------------------------------------------------------------------
select t.parent,t.child,level from test t 
start with t.child = 'ab'
connect by prior t.child = t.parent;
a	ab	1
ab	abc	2
abc	abcd	3

select t.parent,t.child,level from test t 
start with t.child = 'abcd'
connect by prior t.child = t.parent;
abc	abcd	1

--常用,已知子级查询所有父级
select t.parent,t.child,level from test t 
start with t.child = 'abcd'
connect by  t.child = prior t.parent;   
abc	abcd	1
ab	abc	2
a	ab	3

三、pgsql数据库

**数据准备阶段,没有varchar2类型,记得改成varchar

关键字分析,实际先生成一个递归临时表,然后继续进行查询。详情见具体sql和查询结果:

        recursive : 递归的意思

--常用的,已知父级,查询所有子级
with recursive rs as (
select * from test  where parent = 'a'
union all
select a.* from test a, rs where a.parent = rs.child
)
select * from rs ;
1	a	ab
2	a	ac
4	ab	abc
6	ac	acd
5	abc	abcd

with recursive rs as (
select * from test  where child = 'abcd'
union all
select a.* from test a, rs where a.child = rs.parent
)
select * from rs  ;
5	abc	abcd
4	ab	abc
1	a	ab

with recursive rs as (
select * from test  where child = 'abcd'
union all
select a.* from test a, rs where a.parent = rs.child
)
select * from rs  
5	abc	abcd

--常用的,已知子级,查询所有父级
with recursive rs as (
select * from test  where child = 'abcd'
union all
select a.* from test a, rs where a.child = rs.parent
)
select * from rs  
5	abc	abcd
4	ab	abc
1	a	ab

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值