`

2010.06.24——— oracle sql迭代

阅读更多
2010.06.24——— oracle sql迭代

create table tb(id int,name int,pid int)

insert into tb(id ,name,pid) values (1,1,0);
insert into tb(id ,name,pid) values (2,2,0 );
insert into tb(id ,name,pid) values (3,3,0);
insert into tb(id ,name,pid) values (4,4,1);
insert into tb(id ,name,pid) values (5,5,1);
insert into tb(id ,name,pid) values (6,6,2);
insert into tb(id ,name,pid) values (7,7,3);
insert into tb(id ,name,pid) values (8,8,4);
insert into tb(id ,name,pid) values (9,9,4);



id	name	pid
1	1	0
2	2	0
3	3	0
4	4	1
5	5	1
6	6	2
7	7	3



我想找出id为1下面的所有子类的id,不管是几级的

select distinct id from tb
connect by prior id = pid
start with pid = '1'





Oracle的查询语句:


   select mla_parentid, mla_id, mla_name from main_node

start with mla_id=? connect by prior mla_id=mla_parentid

让我们研究这个查询语句:

  本语句实际上是 start with ...connect by 的用法, start with 后面所跟的就是就是递归的种子。在上面的示例中,种子是 mla_id 为 任意传进去的参数

connect by 后面的"prior"如果缺省:则只能查询到符合条件的起始行,并不进行递归查询;

connect by prior 后面所放的字段是有关系的,它指明了查询的方向。如果后面放的是 mla_id=mla_parentid 则表明从本节点查向叶子节点;如果后面放的是 mla_parentid = mla_id则表明从根节点查向本节点;






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics