| 发表于:2007-10-16 10:08:551楼 得分:100 |
db2的递归查询 在db2可以使用sql语句来进行递归查询,就是使用with语句 1.先建一个树形表: - sql code
create table tst (
id integer,
parentid int,
name varchar(20))
2.插入数据 - sql code
insert into tst values
(1,0,'a'),
(2,0,'b'),
(3,1,'c'),
(4,1,'d'),
(5,4,'d'),
(6,5,'d')
3.使用递归查询 - sql code
with rpl (id,parentid,name) as
(
select id,parentid,name from tst where parentid=1
union all
select child.id,child.parentid,child.name from rpl parent, tst child where parent.id=child.parentid
)
select * from rpl
| | |
|