網(wǎng)站首頁 編程語言 正文
1 數(shù)據(jù)準(zhǔn)備
create table area_test(
id number(10) not null,
parent_id number(10),
name varchar2(255) not null
);
alter table area_test add (constraint district_pk primary key (id));
insert into area_test (ID, PARENT_ID, NAME) values (1, null, '中國');
insert into area_test (ID, PARENT_ID, NAME) values (11, 1, '河南省');
insert into area_test (ID, PARENT_ID, NAME) values (12, 1, '北京市');
insert into area_test (ID, PARENT_ID, NAME) values (111, 11, '鄭州市');
insert into area_test (ID, PARENT_ID, NAME) values (112, 11, '平頂山市');
insert into area_test (ID, PARENT_ID, NAME) values (113, 11, '洛陽市');
insert into area_test (ID, PARENT_ID, NAME) values (114, 11, '新鄉(xiāng)市');
insert into area_test (ID, PARENT_ID, NAME) values (115, 11, '南陽市');
insert into area_test (ID, PARENT_ID, NAME) values (121, 12, '朝陽區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (122, 12, '昌平區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1111, 111, '二七區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1112, 111, '中原區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1113, 111, '新鄭市');
insert into area_test (ID, PARENT_ID, NAME) values (1114, 111, '經(jīng)開區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1115, 111, '金水區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1121, 112, '湛河區(qū)');
insert into area_test (ID, PARENT_ID, NAME) values (1122, 112, '舞鋼市');
insert into area_test (ID, PARENT_ID, NAME) values (1123, 112, '寶豐市');
insert into area_test (ID, PARENT_ID, NAME) values (11221, 1122, '尚店鎮(zhèn)');
2 start with connect by prior遞歸查詢
2.1 查詢所有子節(jié)點(diǎn)
select *
from area_test
start with name ='鄭州市'
connect by prior id=parent_id
2.2 查詢所有父節(jié)點(diǎn)
select t.*,level
from area_test t
start with name ='鄭州市'
connect by prior t.parent_id=t.id
order by level asc;
start with 子句:遍歷起始條件,如果要查父結(jié)點(diǎn),這里可以用子結(jié)點(diǎn)的列,反之亦然。
connect by 子句:連接條件。prior 跟父節(jié)點(diǎn)列parentid放在一起,就是往父結(jié)點(diǎn)方向遍歷;prior 跟子結(jié)點(diǎn)列subid放在一起,則往葉子結(jié)點(diǎn)方向遍歷。parent_id、id兩列誰放在“=”前都無所謂,關(guān)鍵是prior跟誰在一起。
order by 子句:排序。
2.3 查詢指定節(jié)點(diǎn)的,根節(jié)點(diǎn)
select d.*,
connect_by_root(d.id) rootid,
connect_by_root(d.name) rootname
from area_test d
where name='二七區(qū)'
start with d.parent_id IS NULL
connect by prior d.id=d.parent_id
2.4 查詢巴中市下行政組織遞歸路徑
select id, parent_id, name, sys_connect_by_path(name, '->') namepath, level
from area_test
start with name = '平頂山市'
connect by prior id = parent_id
3 with遞歸查詢
3.1 with遞歸子類
with tmp(id, parent_id, name)
as (
select id, parent_id, name
from area_test
where name = '平頂山市'
union all
select d.id, d.parent_id, d.name
from tmp, area_test d
where tmp.id = d.parent_id
)
select * from tmp;
3.2 遞歸父類
with tmp(id, parent_id, name)
as
(
select id, parent_id, name
from area_test
where name = '二七區(qū)'
union all
select d.id, d.parent_id, d.name
from tmp, area_test d
where tmp.parent_id = d.id
)
select * from tmp;
補(bǔ)充:實(shí)例
我們稱表中的數(shù)據(jù)存在父子關(guān)系,通過列與列來關(guān)聯(lián)的,這樣的數(shù)據(jù)結(jié)構(gòu)為樹結(jié)構(gòu)。
現(xiàn)在有一個(gè)menu表,字段有id,pid,title三個(gè)。
查詢菜單id為10的所有子菜單。
SELECT * FROM tb_menu m START WITH m.id=10 CONNECT BY m.pid=PRIOR m.id;
將PRIOR關(guān)鍵字放在m.id前面,意思就是查詢pid是當(dāng)前記錄id的記錄,如此順延找到所有子節(jié)點(diǎn)。
查詢菜單id為40的所有父菜單。
SELECT * FROM tb_menu m START WITH m.id=40 CONNECT BY PRIOR m.pid= m.id ORDER BY ID;
總結(jié)
原文鏈接:https://blog.csdn.net/Michael_lcf/article/details/124433725
相關(guān)推薦
- 2022-05-26 mongoDB數(shù)據(jù)庫索引快速入門指南_MongoDB
- 2022-11-16 Docker如何安全地停止和刪除容器_docker
- 2022-06-01 Android自制九宮格解鎖控件_Android
- 2022-07-23 Python代碼實(shí)現(xiàn)雙鏈表_python
- 2022-09-14 Python深入分析@property裝飾器的應(yīng)用_python
- 2022-07-07 Python編寫運(yùn)維進(jìn)程文件目錄操作實(shí)用腳本示例_python
- 2023-07-14 echarts圖表進(jìn)度條類型圖
- 2022-10-01 詳解Python變量與注釋高級(jí)用法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支