Oracle 树结构递归操作遇到问题

来源:百度知道 编辑:UC知道 时间:2024/09/24 14:04:53
我的表是这样
id pid extend
1 0 0
2 1 1
3 2 0
4 3 1
使用
select * from
Table t
connect by prior t.pid=t.id start with t.id=4
可以找到ID为4和4的所有上一级记录.
但是我想判断EXTEND是不是为1的时候就有问题了,因为如果EXTEND不是1那么这次递归就终止
于是这样写
select * from
Table t
connect by prior t.extend=1 and t.pid=t.id start with t.id=4
可是只能找到一条记录,按理说应该找到4 和3才对,不知道这个代码问题出在哪里.

第一次
select * from
Table t where t.extend = '1'
connect by prior t.pid=t.id start with t.id=1

注:extend='1' 条件并不是传递条件,只是个普通条件

第二次
select * from sa_dept where attr2 = '411' connect by prior dept_id = parent_dept_id start with dept_id = '90100000000001'
这是我项目中的一个单位表,可以的啊,你为啥不行

第三次
你的sql问题出现在开始位置不对,开始位置应设在start with dept_id = '90100000000001'
想控制id<4的,就应该把条件加在where 后面
其写法
select * from
Table t where t.extend = '1' and to_number(id)<4
connect by prior t.pid=t.id start with t.id=1