DB2的递归查询

在db2可以使用sql语句来进行递归查询,就是使用with语句

1.先建一个树形表:

None.gifcreate table tst (
None.gifid  
integer,
None.gifparentId 
int,
None.gifname 
varchar(20))

2.插入数据
None.gifinsert into tst values 
None.gif(
1,0,'a'),
None.gif(
2,0,'b'),
None.gif(
3,1,'c'),
None.gif(
4,1,'d'),
None.gif(
5,4,'d'),
None.gif(
6,5,'d')
3.使用递归查询
None.gifwith rpl (id,parentId,name) as 
None.gif(
None.gif
select id,parentId,name from tst  where parentId=1 
None.gif
union all 
None.gif
select  child.id,child.parentId,child.name from rpl parent, tst child where parent.id=child.parentId
None.gif)
None.gif
select * from rpl

这个语句在在db2 7中就有了,在sql2005中才出现.

转载于:https://www.cnblogs.com/yg_zhang/archive/2006/12/09/db2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值