oracle 树形查询函数,oracle 递归查询一个树形结构的菜单

关键字:oracle connect by level 树状结构表

定义如下id 和 名称描述

1—

省长

2—

市长

3—

县长

4— 镇长

5— 村长

其他(6-15)— 平民(没有管辖任何人)

只有

省长、市长和

县长能处理问题,那么每个人出了问题应该

首先找谁来解决?

省长权利最大,自己出了问题自己解决,别人也没法知道; 可以认为在关系表里省长的父节点是自己。

Db代码

3dae436f2dc5ad942383cfb228903c8e.png

create table person(id int primary key, description varchar2(50));

create table relationship(child int, parent int, primary key(child,parent));

insert into person values(1,‘省长’);

insert into person values(2,‘市长’);

insert into person values(3,‘县长’);

insert into person values(4,‘镇长’);

insert into person values(5,‘村长’);

insert into person values(6,‘平民’);

...//省略的都是平民

insert into person values(15,‘平民’);

insert into relationship values(1,1); //关系如下

insert into relationship values(2,1);

insert into relationship values(3,1);

insert into relationship values(5,2);

insert into relationship values(6,2);

insert into relationship values(4,3);

insert into relationship values(7,3);

insert into relationship values(8,3);

insert into relationship values(9,3);

insert into relationship values(10,4);

insert into relationship values(11,4);

insert into relationship values(12,4);

insert into relationship values(13,4);

insert into relationship values(14,5);

insert into relationship values(15,5);

create table person(id int primary key, description varchar2(50));

create table relationship(child int, parent int, primary key(child,parent));

insert into person values(1,‘省长’);

insert into person values(2,‘市长’);

insert into person values(3,‘县长’);

insert into person values(4,‘镇长’);

insert into person values(5,‘村长’);

insert into person values(6,‘平民’);

...//省略的都是平民

insert into person values(15,‘平民’);

insert into relationship values(1,1); //关系如下

insert into relationship values(2,1);

insert into relationship values(3,1);

insert into relationship values(5,2);

insert into relationship values(6,2);

insert into relationship values(4,3);

insert into relationship values(7,3);

insert into relationship values(8,3);

insert into relationship values(9,3);

insert into relationship values(10,4);

insert into relationship values(11,4);

insert into relationship values(12,4);

insert into relationship values(13,4);

insert into relationship values(14,5);

insert into relationship values(15,5);

查看一下关系树:

Oracle代码

e41579219e7a89647003c6e1b6a55a27.png

select rpad('---',(level-1)*3,'---')||child relation_tree

from relationship

start with child=parent

connect by nocycle prior child=parent; --结果如下

RELATION_TREE

------------------

1

---2

------5

---------14

---------15

------6

---3

------4

---------10

---------11

---------12

---------13

------7

------8

------9

已选择15行。

select rpad('---',(level-1)*3,'---')||child relation_tree

from relationship

start with child=parent

connect by nocycle prior child=parent; --结果如下

RELATION_TREE

------------------

1

---2

------5

---------14

---------15

------6

---3

------4

---------10

---------11

---------12

---------13

------7

------8

------9

已选择15行。

查看父子关系情况:

Oracle代码

d7403fd9677acf64de58c9895e1f45a5.png

select child,parent

from relationship

start with child=parent

connect by nocycle prior child=parent

order by parent; --结果如下

CHILD     PARENT

---------- ----------

11

21

31

52

62

43

83

73

93

114

124

104

134

145

155

已选择15行。

select child,parent

from relationship

start with child=parent

connect by nocycle prior child=parent

order by parent; --结果如下

CHILD PARENT

---------- ----------

1 1

2 1

3 1

5 2

6 2

4 3

8 3

7 3

9 3

11 4

12 4

10 4

13 4

14 5

15 5

已选择15行。

下面要看一看:每个人有事时,首先找到谁来处理?不能每个人有事都找省长吧。

下面的sql使用了oracle家的两个变态函数:first_value & connect_by_root

Oracle代码

18d2f4a463d93feeaf39ebbf34020762.png

select distinct child ,first_value(parent)over(partition by child order by lv) parent

from(

select connect_by_root(r.child) child, p.description descr, level lv, r.parent

from person p ,relationship r

where p.id= r.parent

connect by nocycle prior r.parent=r.child

)

where descr in('省长','市长','县长')

order by parent,child; --结果如下

CHILD     PARENT

---------- ----------

11

21

31

52

62

142

152

43

73

83

93

103

113

123

133

已选择15行。

select distinct child ,first_value(parent)over(partition by child order by lv) parent

from(

select connect_by_root(r.child) child, p.description descr, level lv, r.parent

from person p ,relationship r

where p.id= r.parent

connect by nocycle prior r.parent=r.child

)

where descr in('省长', '市长', '县长')

order by parent,child; --结果如下

CHILD PARENT

---------- ----------

1 1

2 1

3 1

5 2

6 2

14 2

15 2

4 3

7 3

8 3

9 3

10 3

11 3

12 3

13 3

已选择15行。

这个sql到底行不行,再加条数据看看

Db代码

7f18690229d6e8ab7096258c81446483.png

insert into person values(333,‘县长’);

insert into person values(555,‘村长’);

insert into person values(666,‘平民’);

insert into person values(777,‘平民’);

insert into relationship values(333,1);

insert into relationship values(555,333);

insert into relationship values(666,555);

insert into relationship values(777,666);

--666这个平民有¥,777愿意跟着他(这条记录比较特殊)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值