大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Oracle如何实现层次化查询

小编给大家分享一下Oracle如何实现层次化查询,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了东兰免费建站欢迎大家使用!

Oracle层次化查询

(1)树状模型

假如有一个市场模型,其中一级市场是中国,二级市场是省份,三级市场是各个省下面的市,四级市场是各个市下面的县。那么可以创建表market:

create table market(market_id number(2), market_name varchar2(10), parent_market_id number(2));

insert all

into market values(1, 'China', 0)

into market values(2, 'Shan Xi', 1)

into market values(3, 'Shan Dong', 1)

into market values(4, 'Fu Jian', 1)

into market values(5, 'Tai Yuan', 2)

into market values(6, 'Yang Quan', 2)

into market values(7, 'Qi Zhou', 2)

into market values(8, 'Qing Dao', 3)

into market values(9, 'Ji Nan', 3)

into market values(10, 'Yan Tai', 3)

into market values(11, 'Xia Men', 4)

into market values(12, 'Fu Zhou', 4)

into market values(13, 'Quan Zhou', 4)

into market values(14, 'Gu Jiao', 5)

into market values(15, 'Yang Qu', 5)

into market values(16, 'Qing Xu', 5)

select * from dual;

(2)层次化查询

select market_id, market_name

from market

start with market_name = 'Shan Xi'

connect by prior market_id = parent_market_id;

MARKET_ID MARKET_NAME

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

2 Shan Xi

5 Tai Yuan

14 Gu Jiao

15 Yang Qu

16 Qing Xu

6 Yang Quan

7 Qi Zhou

7 rows selected.

这里的搜索规则为:从'Shan Xi'节点开始搜索、前一个节点的market_id等于下一个节点的parent_markket_id。

(3)相关函数

显示搜索路径的函数为

sys_connect_by_path(列名,分隔符)

示例:

select market_id, market_name, sys_connect_by_path(market_name, '/') as market_path

from market

start with market_name = 'Gu Jiao'

connect by prior parent_market_id = market_id;

MARKET_ID MARKET_NAM MARKET_PATH

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

14 Gu Jiao    /Gu Jiao

5 Tai Yuan   /Gu Jiao/Tai Yuan

2 Shan Xi    /Gu Jiao/Tai Yuan/Shan Xi

1 China      /Gu Jiao/Tai Yuan/Shan Xi/China

这里可以利用max()函数来获得完整的路径:

select max(sys_connect_by_path(market_name, '/')) market_path

from market

start with market_name = 'Gu Jiao'

connect by prior parent_market_id = market_id;

MARKET_PATH

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

以上是“Oracle如何实现层次化查询”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网站栏目:Oracle如何实现层次化查询
当前URL:http://dzwzjz.com/article/igdhcg.html
在线咨询
服务热线
服务热线:028-86922220
TOP