查询:select distinct grpentpid from t_grptogrpentp where grpid in (select distinct grpentpid from t_grptogrpentp start with grpid = '1' connect by prior grpentpid||grpentptype = grpid||'1') and grpentptype = '2'


creat table grptogrpentp (toid char(5), grpid char(5), grpentpid char(5), grpentptype char(1));
insert into grptogrpentp values('01','01','02','1');
insert into grptogrpentp values('02','01','03','1');
insert into grptogrpentp values('03','01','04','2');
insert into grptogrpentp values('04','02','05','2');
insert into grptogrpentp values('05','02','06','2');
insert into grptogrpentp values('06','03','07','2');


toid 主键id
grpid 父节点id
grpentpid 子结点id
grpentptype 标志是否为最终子结点,1表示不是最终的,其下仍有子结点,2表示为最终子结点
说明:要查找莫个grpid下的所有最终子结点,使用start with...connect by 原本上是可以用递归完成这个功能的,但是由于自己用的是9i,所以有一个bug,ORC-01436错误,这个错误的原因是grpid=grpentpid,如果是10g的话,使用start with...connect by nocycle prior就可以搞定,但是9i中不支持,因此,人为的写了我上面的sql,使用了联合键来避免grpid=grpentpid,仅仅完成自己的要求,没有什么可说的,使用distinct过滤了重复的数据
评论
hydex 2008-04-08
一个值得读一下的文章:http://www.itpub.net/redirect.php?tid=392622&goto=lastpost
发表评论

您还没有登录,请登录后发表评论

hydex
搜索本博客
我的相册
5e0be7a5-3ea9-31fe-a7c4-7a19f3b7d537-thumb
自行车
共 1 张
存档
最新评论