oracle递归查询固定字符拼接,Oracle 树结构(递归查询 start with和connect by 的用法)...

递归查询: 使用语句SQL语句即可把整个递归树全部查询出来。

1.语法:

SELECT [LEVEL], column, expr...

FROM table

[WHERE condition(s)]

[START WITH condition(s)]

[CONNECT BY PRIOR condition(s)] ;

--语法

1 select … from tablename

2 start with 条件1

3 connect by 条件2

4 where 条件3;

简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:org_id,parent_id,那么通过表示每一条记录的parent是谁,就可以形成一个树状结构,用上述语法的查询可以取得这棵树的所有记录,其中:

条件1 是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。

条件2 是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR org_id = parent_id;就是说上一条记录的org_id 是本条记录的parent_id,即本记录的父亲是上一条记录。

条件3 是过滤条件,用于对返回的所有记录进行过滤。

简单介绍如下:

在扫描树结构表时,需要依此访问树结构的每个节点,一个节点只能访问一次,其访问的步骤如下:

第一步:从根节点开始;

第二步:访问该节点;

第三步:判断该节点有无未被访问的子节点,若有,则转向它最左侧的未被访问的子节,并执行第二步,否则执行第四步;

第四步:若该节点为根节点,则访问完毕,否则执行第五步;

第五步:返回到该节点的父节点,并执行第三步骤。

总之:扫描整个树结构的过程也即是中序遍历树的过程。

2.例子:

--递归查询(树结构)

select prior t.item_id,

prior t.caption_explain,

t.item_id,

t.caption_explain

from bwptest1.sys_tree_list t

start with t.item_id = 'flow_menu'

connect by prior t.item_id = t.parent_id;

--1.主键ID,parent_id

--2.prior 的位置

--connect by prior 主键ID 从上往下

--connect by prior parent_id 从下往上

--树结构,从上往下遍历

select prior t.item_id,

prior t.caption_explain,

t.item_id,

t.caption_explain

from bwptest1.sys_tree_list t

start with t.item_id = 'flow_menu'

connect by prior t.item_id = t.parent_id;

--树结构,从下往上遍历

select prior t.item_id,

prior t.caption_explain,

t.item_id,

t.caption_explain

from bwptest1.sys_tree_list t

start with t.item_id = 'flow_process_manage'

connect by prior t.parent_id = t.item_id;

select prior t.item_id,

prior t.caption_explain,

t.item_id,

t.caption_explain

from bwptest1.sys_tree_list t

start with t.item_id = 'flow_process_manage'

connect by t.item_id = prior t.parent_id;

--使用level 和 lpad函数,在output中显示树形层次(注意中文字符 lengthb,英文字符 length)

SELECT prior t.item_id,

prior t.caption_explain,

t.item_id,

t.caption_explain,

level,

lengthb(prior t.caption_explain),

lengthb(prior t.caption_explain) + (level * 2)-2,

lpad(t.caption_explain,

lengthb(t.caption_explain) + (level * 2)-2,

'_') chart

FROM bwptest1.sys_tree_list t

start with t.item_id = 'flow_menu'

connect by prior t.item_id = t.parent_id;

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值