2008-03-26
不规范的树结构表查询
查询: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过滤了重复的数据
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过滤了重复的数据
- 15:45
- 浏览 (95)
- 评论 (1)
- 分类: Oracle SQL
- 相关推荐
发表评论
- 浏览: 9973 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
自行车
共 1 张
共 1 张
最近加入圈子
最新评论
-
HashMap使用
你就是在thinking in java 上下载的吧,不过总结的不错
-- by lysmart_8 -
过去有一位年轻和尚,一心 ...
哈哈哈,这个老和尚估计是个高级“和尚”
-- by hydex -
不规范的树结构表查询
一个值得读一下的文章:http://www.itpub.net/redirect ...
-- by hydex






评论排行榜