這篇文章給大家介紹怎么在Oracle中實(shí)現(xiàn)遞歸樹(shù)形結(jié)構(gòu)查詢功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站建設(shè)、成都做網(wǎng)站和棕樹(shù)數(shù)據(jù)中心的網(wǎng)絡(luò)公司,有著豐富的建站經(jīng)驗(yàn)和案例。
oracle樹(shù)狀結(jié)構(gòu)查詢即層次遞歸查詢,是sql語(yǔ)句經(jīng)常用到的,在實(shí)際開(kāi)發(fā)中組織結(jié)構(gòu)實(shí)現(xiàn)及其層次化實(shí)現(xiàn)功能也是經(jīng)常遇到的。
概要:樹(shù)狀結(jié)構(gòu)通常由根節(jié)點(diǎn)、父節(jié)點(diǎn)、子節(jié)點(diǎn)和葉節(jié)點(diǎn)組成,簡(jiǎn)單來(lái)說(shuō),一張表中存在兩個(gè)字段,dept_id,par_dept_id,那么通過(guò)找到每一條記錄的父級(jí)id即可形成一個(gè)樹(shù)狀結(jié)構(gòu),也就是par_dept_id(子)=dept_id(父),
通俗的說(shuō)就是這條記錄的par_dept_id是另外一條記錄也就是父級(jí)的dept_id,其樹(shù)狀結(jié)構(gòu)層級(jí)查詢的基本語(yǔ)法是:
SELECT [LEVEL],* FEOM table_name START WITH 條件1 CONNECT BY PRIOR 條件2 WHERE 條件3 ORDER BY 排序字段
說(shuō)明:LEVEL---偽列,用于表示樹(shù)的層次
條件1---根節(jié)點(diǎn)的限定條件,當(dāng)然也可以放寬權(quán)限,以獲得多個(gè)根節(jié)點(diǎn),也就是獲取多個(gè)樹(shù)
條件2---連接條件,目的就是給出父子之間的關(guān)系是什么,根據(jù)這個(gè)關(guān)系進(jìn)行遞歸查詢
條件3---過(guò)濾條件,對(duì)所有返回的記錄進(jìn)行過(guò)濾。
排序字段---對(duì)所有返回記錄進(jìn)行排序
對(duì)prior說(shuō)明:要的時(shí)候有兩種寫法:connect by prior dept_id=par_dept_id
或 connect by dept_id=prior par_dept_id
,前一種寫法表示采用自上而下的搜索方式(先找父節(jié)點(diǎn)然后找子節(jié)點(diǎn)),后一種寫法表示采用自下而上的搜索方式(先找葉子節(jié)點(diǎn)然后找父節(jié)點(diǎn))。
樹(shù)狀結(jié)構(gòu)層次化查詢需要對(duì)樹(shù)結(jié)構(gòu)的每一個(gè)節(jié)點(diǎn)進(jìn)行訪問(wèn)并且不能重復(fù),其訪問(wèn)步驟為:
大致意思就是掃描整個(gè)樹(shù)結(jié)構(gòu)的過(guò)程即遍歷樹(shù)的過(guò)程,其用語(yǔ)言描述就是:
步驟一:從根節(jié)點(diǎn)開(kāi)始;
步驟二:訪問(wèn)該節(jié)點(diǎn);
步驟三:判斷該節(jié)點(diǎn)有無(wú)未被訪問(wèn)的子節(jié)點(diǎn),若有,則轉(zhuǎn)向它最左側(cè)的未被訪問(wèn)的子節(jié),并執(zhí)行第二步,否則執(zhí)行第四步;
步驟四:若該節(jié)點(diǎn)為根節(jié)點(diǎn),則訪問(wèn)完畢,否則執(zhí)行第五步;
步驟五:返回到該節(jié)點(diǎn)的父節(jié)點(diǎn),并執(zhí)行第三步驟。
除此之外,sys_connect_by_path函數(shù)是和connect by 一起使用的,在實(shí)戰(zhàn)中具體帶目的具體介紹!
實(shí)戰(zhàn):最近做項(xiàng)目的組織結(jié)構(gòu),對(duì)于部門的各級(jí)層次顯示,由于這部分掌握不牢固,用最笨的like模糊查詢解決了,雖然功能實(shí)現(xiàn)了,但是問(wèn)題很多,如擴(kuò)展性不好,稍微改下需求就要進(jìn)行大改,不滿意最后對(duì)其進(jìn)行了優(yōu)化。在開(kāi)發(fā)中能用數(shù)據(jù)庫(kù)解決的就不要用java去解決,這也是我一直保持的想法并堅(jiān)持著。
創(chuàng)建表:
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 '部門信息,和單位多對(duì)一'; -- 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 '父級(jí)部門ID'; comment on column SYS_DEPT.dept_leader is '部門領(lǐng)導(dǎo)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:無(wú)效;1:有效'; comment on column SYS_DEPT.bqq_dept_id is '企業(yè)qqdeptid'; comment on column SYS_DEPT.bqq_par_dept_id is '企業(yè)qq父類deptid';
插入測(cè)試數(shù)據(jù):
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', '客運(yùn)部', '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', '生產(chǎn)調(diào)度', '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', '站務(wù)中心', '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', '蘭州大學(xué)站', '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', '東方紅廣場(chǎng)站', '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', '西關(guān)站', '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', '蘭州西站北廣場(chǎng)站', '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', '蘭州海關(guān)站', '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', '蘭州城市學(xué)院(省科技館)站', '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', '陳官營(yíng)站', '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', '乘務(wù)中心', '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', '陳官營(yíng)車場(chǎng)組', '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', '東崗車場(chǎng)組', '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', '第一車隊(duì)', '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', '第二車隊(duì)', '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', '第三車隊(duì)', '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', '第四車隊(duì)', '110003006', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 161930', '402881e54c40d74d014c40d8407a0016', '1', 164, '1', null, null);
在這張表中有三個(gè)字段:dept_id 部門主鍵id;dept_name 部門名稱;dept_code 部門編碼;par_dept_id 父級(jí)部門id(首級(jí)部門為 -1); 當(dāng)前節(jié)點(diǎn)遍歷子節(jié)點(diǎn)(遍歷當(dāng)前部門下所有子部門包括本身)
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
結(jié)果:
dept_id=40288ac45a3c1e8b015a3c28b4ae01d6
是客運(yùn)部主鍵,對(duì)其下的所有子部門進(jìn)行遍歷,同時(shí)用 order by level,dept_code
進(jìn)行排序 以便達(dá)到實(shí)際生活中想要的數(shù)據(jù);共31條數(shù)據(jù),部分?jǐn)?shù)據(jù)如圖所示:
但是:
有問(wèn)題啊,如果你想在上面的數(shù)據(jù)中獲取層級(jí)在2也就是level=2的所有部門,發(fā)現(xiàn)剛開(kāi)始的時(shí)候介紹的語(yǔ)言不起作用?并且會(huì)報(bào)ORA-00933:sql命令未正確結(jié)束,why?
這個(gè)我暫時(shí)也沒(méi)有得到研究出理論知識(shí),但是改變下where level='2'的位置發(fā)現(xiàn)才會(huì)可以的。錯(cuò)誤的和正確的sql我們對(duì)比一下,以后會(huì)用就行,要是路過(guò)的大神知道為什么,還請(qǐng)告知下,萬(wàn)分感謝!
錯(cuò)誤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
當(dāng)然了,這個(gè)對(duì)其他形式的where過(guò)濾所有返回記錄沒(méi)有影響的,這個(gè)只是一個(gè)例外!
sys_connect_by_path
函數(shù)求父節(jié)點(diǎn)到子節(jié)點(diǎn)路徑
簡(jiǎn)單介紹下,在oracle中sys_connect_by_path
與connect by 一起使用,也就是先要有或建立一棵樹(shù),否則無(wú)用還會(huì)報(bào)錯(cuò)。它的主要作用體現(xiàn)在path上即路徑,是可以吧一個(gè)父節(jié)點(diǎn)下的所有節(jié)點(diǎn)通過(guò)某個(gè)字符區(qū)分,然后鏈接在一個(gè)列中顯示。
sys_connect_by_path(column,clear),
其中column是字符型或能自動(dòng)轉(zhuǎn)換成字符型的列名,它的主要目的就是將父節(jié)點(diǎn)到當(dāng)前節(jié)點(diǎn)的“path”按照指定的模式出現(xiàn),char可以是單字符也可以是多字符,但不能使用列值中包含的字符,而且這個(gè)參數(shù)必須是常量,且不允許使用綁定變量,clear不要用逗號(hào)。
文字容易讓人疲勞,放圖和代碼吧
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
結(jié)果:
下面以最簡(jiǎn)單的情況進(jìn)行示例說(shuō)明:
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;
關(guān)于怎么在Oracle中實(shí)現(xiàn)遞歸樹(shù)形結(jié)構(gòu)查詢功能就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享題目:怎么在Oracle中實(shí)現(xiàn)遞歸樹(shù)形結(jié)構(gòu)查詢功能
當(dāng)前路徑:http://m.rwnh.cn/article38/jippsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、服務(wù)器托管、搜索引擎優(yōu)化、虛擬主機(jī)、響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)