您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Oracle中實現遞歸樹形結構查詢功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
oracle樹狀結構查詢即層次遞歸查詢,是sql語句經常用到的,在實際開發中組織結構實現及其層次化實現功能也是經常遇到的。
概要:樹狀結構通常由根節點、父節點、子節點和葉節點組成,簡單來說,一張表中存在兩個字段,dept_id,par_dept_id,那么通過找到每一條記錄的父級id即可形成一個樹狀結構,也就是par_dept_id(子)=dept_id(父),
通俗的說就是這條記錄的par_dept_id是另外一條記錄也就是父級的dept_id,其樹狀結構層級查詢的基本語法是:
SELECT [LEVEL],* FEOM table_name START WITH 條件1 CONNECT BY PRIOR 條件2 WHERE 條件3 ORDER BY 排序字段
說明:LEVEL---偽列,用于表示樹的層次
條件1---根節點的限定條件,當然也可以放寬權限,以獲得多個根節點,也就是獲取多個樹
條件2---連接條件,目的就是給出父子之間的關系是什么,根據這個關系進行遞歸查詢
條件3---過濾條件,對所有返回的記錄進行過濾。
排序字段---對所有返回記錄進行排序
對prior說明:要的時候有兩種寫法:connect by prior dept_id=par_dept_id
或 connect by dept_id=prior par_dept_id
,前一種寫法表示采用自上而下的搜索方式(先找父節點然后找子節點),后一種寫法表示采用自下而上的搜索方式(先找葉子節點然后找父節點)。
樹狀結構層次化查詢需要對樹結構的每一個節點進行訪問并且不能重復,其訪問步驟為:
大致意思就是掃描整個樹結構的過程即遍歷樹的過程,其用語言描述就是:
步驟一:從根節點開始;
步驟二:訪問該節點;
步驟三:判斷該節點有無未被訪問的子節點,若有,則轉向它最左側的未被訪問的子節,并執行第二步,否則執行第四步;
步驟四:若該節點為根節點,則訪問完畢,否則執行第五步;
步驟五:返回到該節點的父節點,并執行第三步驟。
除此之外,sys_connect_by_path函數是和connect by 一起使用的,在實戰中具體帶目的具體介紹!
實戰:最近做項目的組織結構,對于部門的各級層次顯示,由于這部分掌握不牢固,用最笨的like模糊查詢解決了,雖然功能實現了,但是問題很多,如擴展性不好,稍微改下需求就要進行大改,不滿意最后對其進行了優化。在開發中能用數據庫解決的就不要用java去解決,這也是我一直保持的想法并堅持著。
創建表:
create table SYS_DEPT ( dept_id VARCHAR2(32) not null, dept_name VARCHAR2(128), dept_code VARCHAR2(32), par_dept_id VARCHAR2(32), dept_leader VARCHAR2(32), dept_desc VARCHAR2(256), create_time CHAR(19), org_id VARCHAR2(32), dept_type VARCHAR2(1), order_id NUMBER, state CHAR(1) default '1', bqq_dept_id VARCHAR2(128), bqq_par_dept_id VARCHAR2(128) ) -- Add comments to the table comment on table SYS_DEPT is '部門信息,和單位多對一'; -- Add comments to the columns comment on column SYS_DEPT.dept_id is '主鍵'; comment on column SYS_DEPT.dept_name is '名稱'; comment on column SYS_DEPT.dept_code is '編碼,用于遞歸'; comment on column SYS_DEPT.par_dept_id is '父級部門ID'; comment on column SYS_DEPT.dept_leader is '部門領導ID'; comment on column SYS_DEPT.dept_desc is '部門描述'; comment on column SYS_DEPT.create_time is 'yyyy-mm-dd HHMMSS'; comment on column SYS_DEPT.org_id is '單位ID'; comment on column SYS_DEPT.dept_type is '1:正式部門;2:虛擬部門(用于通訊錄展示)'; comment on column SYS_DEPT.order_id is '排序字段'; comment on column SYS_DEPT.state is '0:無效;1:有效'; comment on column SYS_DEPT.bqq_dept_id is '企業qqdeptid'; comment on column SYS_DEPT.bqq_par_dept_id is '企業qq父類deptid';
插入測試數據:
undefined
insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('40288ac45a3c1e8b015a3c28b4ae01d6', '客運部', '110', '-1', null, null, '2017-02-14 182625', '402881e54c40d74d014c40d8407a0016', '1', 29, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b60f98a1d59b3', '綜合室', '110001', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 150338', '402881e54c40d74d014c40d8407a0016', '1', 63, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6134d9ff2946', '生產調度', '110001001', '4028e4d35b5ca4ee015b60f98a1d59b3', null, null, '2017-04-12 160825', '402881e54c40d74d014c40d8407a0016', '1', 135, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b60f9fae95a44', '站務中心', '110002', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 150407', '402881e54c40d74d014c40d8407a0016', '1', 64, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613562be2a08', '東崗站', '110002001', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 160900', '402881e54c40d74d014c40d8407a0016', '1', 136, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6135f9de2aca', '焦家灣站', '110002002', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 160939', '402881e54c40d74d014c40d8407a0016', '1', 137, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6136a3e22bb2', '拱星墩站', '110002003', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161022', '402881e54c40d74d014c40d8407a0016', '1', 138, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613723bb2c5f', '省氣象局站', '110002004', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161055', '402881e54c40d74d014c40d8407a0016', '1', 139, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6137a5772d06', '五里鋪站', '110002005', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161128', '402881e54c40d74d014c40d8407a0016', '1', 140, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6137e4e72d57', '蘭州大學站', '110002006', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161144', '402881e54c40d74d014c40d8407a0016', '1', 141, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613840112dd0', '東方紅廣場站', '110002007', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161208', '402881e54c40d74d014c40d8407a0016', '1', 142, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6138765c2e12', '省政府站', '110002008', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161221', '402881e54c40d74d014c40d8407a0016', '1', 143, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6138b84b2e68', '西關站', '110002009', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161238', '402881e54c40d74d014c40d8407a0016', '1', 145, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6139390e2f06', '文化宮站', '110002010', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161311', '402881e54c40d74d014c40d8407a0016', '1', 146, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613980a82f61', '小西湖站', '110002011', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161330', '402881e54c40d74d014c40d8407a0016', '1', 147, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b6139c1dc2fb4', '七里河站', '110002012', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161346', '402881e54c40d74d014c40d8407a0016', '1', 148, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613a24853047', '西站十字站', '110002013', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161412', '402881e54c40d74d014c40d8407a0016', '1', 149, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613a81f030ce', '蘭州西站北廣場站', '110002014', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161436', '402881e54c40d74d014c40d8407a0016', '1', 150, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613ad627313d', '土門墩站', '110002015', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161457', '402881e54c40d74d014c40d8407a0016', '1', 151, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613b394c31c6', '馬灘站', '110002016', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161522', '402881e54c40d74d014c40d8407a0016', '1', 152, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613b9051325e', '蘭州海關站', '110002017', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161545', '402881e54c40d74d014c40d8407a0016', '1', 153, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613c286b332e', '蘭州城市學院(省科技館)站', '110002018', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161624', '402881e54c40d74d014c40d8407a0016', '1', 154, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613c806933a3', '深安大橋南站', '110002019', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161646', '402881e54c40d74d014c40d8407a0016', '1', 155, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613cdf98342c', '陳官營站', '110002020', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 161711', '402881e54c40d74d014c40d8407a0016', '1', 157, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b60fa3e2f5a94', '乘務中心', '110003', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 150424', '402881e54c40d74d014c40d8407a0016', '1', 65, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613d738d34f4', '陳官營車場組', '110003001', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161748', '402881e54c40d74d014c40d8407a0016', '1', 158, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613defed359e', '東崗車場組', '110003002', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161820', '402881e54c40d74d014c40d8407a0016', '1', 159, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613e42ae3612', '第一車隊', '110003003', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161841', '402881e54c40d74d014c40d8407a0016', '1', 161, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613e7a50366c', '第二車隊', '110003004', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161856', '402881e54c40d74d014c40d8407a0016', '1', 162, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613ebc8e36c1', '第三車隊', '110003005', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161913', '402881e54c40d74d014c40d8407a0016', '1', 163, '1', null, null); insert into SYS_DEPT (DEPT_ID, DEPT_NAME, DEPT_CODE, PAR_DEPT_ID, DEPT_LEADER, DEPT_DESC, CREATE_TIME, ORG_ID, DEPT_TYPE, ORDER_ID, STATE, BQQ_DEPT_ID, BQQ_PAR_DEPT_ID) values ('4028e4d35b5ca4ee015b613eff483729', '第四車隊', '110003006', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161930', '402881e54c40d74d014c40d8407a0016', '1', 164, '1', null, null);
在這張表中有三個字段:dept_id 部門主鍵id;dept_name 部門名稱;dept_code 部門編碼;par_dept_id 父級部門id(首級部門為 -1); 當前節點遍歷子節點(遍歷當前部門下所有子部門包括本身)
select t.dept_id, t.dept_name, t.dept_code, t.par_dept_id, level from SYS_DEPT t start with t.dept_id = '40288ac45a3c1e8b015a3c28b4ae01d6' connect by prior t.dept_id = t.par_dept_id order by level, t.dept_code
結果:
dept_id=40288ac45a3c1e8b015a3c28b4ae01d6
是客運部主鍵,對其下的所有子部門進行遍歷,同時用 order by level,dept_code
進行排序 以便達到實際生活中想要的數據;共31條數據,部分數據如圖所示:
但是:
有問題啊,如果你想在上面的數據中獲取層級在2也就是level=2的所有部門,發現剛開始的時候介紹的語言不起作用?并且會報ORA-00933:sql命令未正確結束,why?
這個我暫時也沒有得到研究出理論知識,但是改變下where level='2'的位置發現才會可以的。錯誤的和正確的sql我們對比一下,以后會用就行,要是路過的大神知道為什么,還請告知下,萬分感謝!
錯誤sql:
select t.dept_id, t.dept_name, t.dept_code, t.par_dept_id, level from SYS_DEPT t start with t.dept_id = '40288ac45a3c1e8b015a3c28b4ae01d6' connect by prior t.dept_id = t.par_dept_id where level = '2' order by level, t.dept_code
正確sql:
select t.dept_id, t.dept_name, t.dept_code, t.par_dept_id, level from SYS_DEPT t where level = '2' start with t.dept_id = '40288ac45a3c1e8b015a3c28b4ae01d6' connect by prior t.dept_id = t.par_dept_id order by level, t.dept_code
undefined
當然了,這個對其他形式的where過濾所有返回記錄沒有影響的,這個只是一個例外!
sys_connect_by_path
函數求父節點到子節點路徑
簡單介紹下,在oracle中sys_connect_by_path
與connect by 一起使用,也就是先要有或建立一棵樹,否則無用還會報錯。它的主要作用體現在path上即路徑,是可以吧一個父節點下的所有節點通過某個字符區分,然后鏈接在一個列中顯示。
sys_connect_by_path(column,clear),
其中column是字符型或能自動轉換成字符型的列名,它的主要目的就是將父節點到當前節點的“path”按照指定的模式出現,char可以是單字符也可以是多字符,但不能使用列值中包含的字符,而且這個參數必須是常量,且不允許使用綁定變量,clear不要用逗號。
文字容易讓人疲勞,放圖和代碼吧
select sys_connect_by_path(t.dept_name,'-->'),t.dept_id, t.dept_name, t.dept_code, t.par_dept_id, level from SYS_DEPT t start with t.dept_id = '40288ac45a3c1e8b015a3c28b4ae01d6' connect by prior t.dept_id = t.par_dept_id order by level, t.dept_code
結果:
下面以最簡單的情況進行示例說明:
SELECT t.f_id, SYS_CONNECT_BY_PATH(t.f_id, '\') AS con_code, SYS_CONNECT_BY_PATH(t.f_name, '\') AS con_name FROM 表名 t START WITH t.f_pid IS NULL CONNECT BY PRIOR t.f_id = t.f_pid;
關于怎么在Oracle中實現遞歸樹形結構查詢功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。