引用:https://blog.csdn.net/u012615705/article/details/78321022 文章轉自上述地址,內部有稍許改動,如有需要請查看原文。 oracle樹狀結構查詢即層次遞歸查詢,是sql語句經常用到的,在實際開發中組織結構實現及其層次化實現功能也是經常遇到的。... ...
引用:https://blog.csdn.net/u012615705/article/details/78321022 文章轉自上述地址,內部有稍許改動,如有需要請查看原文。
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去解決,這也是我一直保持的想法並堅持著。
創建表:
1: create table SYS_DEPT
2: (
3: dept_id VARCHAR2(32) not null,
4: dept_name VARCHAR2(128),
5: dept_code VARCHAR2(32),
6: par_dept_id VARCHAR2(32),
7: dept_leader VARCHAR2(32),
8: dept_desc VARCHAR2(256),
9: create_time CHAR(19),
10: org_id VARCHAR2(32),
11: dept_type VARCHAR2(1),
12: order_id NUMBER,
13: state CHAR(1) default '1',
14: bqq_dept_id VARCHAR2(128),
15: bqq_par_dept_id VARCHAR2(128)
16: )
17:
18: -- Add comments to the table
19: comment on table SYS_DEPT
20: is '部門信息,和單位多對一';
21: -- Add comments to the columns
22: comment on column SYS_DEPT.dept_id
23: is '主鍵';
24: comment on column SYS_DEPT.dept_name
25: is '名稱';
26: comment on column SYS_DEPT.dept_code
27: is '編碼,用於遞歸';
28: comment on column SYS_DEPT.par_dept_id
29: is '父級部門ID';
30: comment on column SYS_DEPT.dept_leader
31: is '部門領導ID';
32: comment on column SYS_DEPT.dept_desc
33: is '部門描述';
34: comment on column SYS_DEPT.create_time
35: is 'yyyy-mm-dd HH:MM:SS';
36: comment on column SYS_DEPT.org_id
37: is '單位ID';
38: comment on column SYS_DEPT.dept_type
39: is '1:正式部門;2:虛擬部門(用於通訊錄展示)';
40: comment on column SYS_DEPT.order_id
41: is '排序欄位';
42: comment on column SYS_DEPT.state
43: is '0:無效;1:有效';
44: comment on column SYS_DEPT.bqq_dept_id
45: is '企業qqdeptid';
46: comment on column SYS_DEPT.bqq_par_dept_id
47: is '企業qq父類deptid';
插入測試數據:
1: 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)
2: values ('40288ac45a3c1e8b015a3c28b4ae01d6', '客運部', '110', '-1', null, null, '2017-02-14 18:26:25', '402881e54c40d74d014c40d8407a0016', '1', 29, '1', null, null);
3:
4: 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)
5: values ('4028e4d35b5ca4ee015b60f98a1d59b3', '綜合室', '110001', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 15:03:38', '402881e54c40d74d014c40d8407a0016', '1', 63, '1', null, null);
6:
7: 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)
8: values ('4028e4d35b5ca4ee015b6134d9ff2946', '生產調度', '110001001', '4028e4d35b5ca4ee015b60f98a1d59b3', null, null, '2017-04-12 16:08:25', '402881e54c40d74d014c40d8407a0016', '1', 135, '1', null, null);
9:
10: 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)
11: values ('4028e4d35b5ca4ee015b60f9fae95a44', '站務中心', '110002', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 15:04:07', '402881e54c40d74d014c40d8407a0016', '1', 64, '1', null, null);
12:
13: 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)
14: values ('4028e4d35b5ca4ee015b613562be2a08', '東崗站', '110002001', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:09:00', '402881e54c40d74d014c40d8407a0016', '1', 136, '1', null, null);
15:
16: 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)
17: values ('4028e4d35b5ca4ee015b6135f9de2aca', '焦家灣站', '110002002', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:09:39', '402881e54c40d74d014c40d8407a0016', '1', 137, '1', null, null);
18:
19: 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)
20: values ('4028e4d35b5ca4ee015b6136a3e22bb2', '拱星墩站', '110002003', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:10:22', '402881e54c40d74d014c40d8407a0016', '1', 138, '1', null, null);
21:
22: 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)
23: values ('4028e4d35b5ca4ee015b613723bb2c5f', '省氣象局站', '110002004', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:10:55', '402881e54c40d74d014c40d8407a0016', '1', 139, '1', null, null);
24:
25: 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)
26: values ('4028e4d35b5ca4ee015b6137a5772d06', '五里鋪站', '110002005', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:11:28', '402881e54c40d74d014c40d8407a0016', '1', 140, '1', null, null);
27:
28: 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)
29: values ('4028e4d35b5ca4ee015b6137e4e72d57', '蘭州大學站', '110002006', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:11:44', '402881e54c40d74d014c40d8407a0016', '1', 141, '1', null, null);
30:
31: 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)
32: values ('4028e4d35b5ca4ee015b613840112dd0', '東方紅廣場站', '110002007', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:12:08', '402881e54c40d74d014c40d8407a0016', '1', 142, '1', null, null);
33:
34: 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)
35: values ('4028e4d35b5ca4ee015b6138765c2e12', '省政府站', '110002008', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:12:21', '402881e54c40d74d014c40d8407a0016', '1', 143, '1', null, null);
36:
37: 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)
38: values ('4028e4d35b5ca4ee015b6138b84b2e68', '西關站', '110002009', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:12:38', '402881e54c40d74d014c40d8407a0016', '1', 145, '1', null, null);
39:
40: 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)
41: values ('4028e4d35b5ca4ee015b6139390e2f06', '文化宮站', '110002010', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:13:11', '402881e54c40d74d014c40d8407a0016', '1', 146, '1', null, null);
42:
43: 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)
44: values ('4028e4d35b5ca4ee015b613980a82f61', '小西湖站', '110002011', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:13:30', '402881e54c40d74d014c40d8407a0016', '1', 147, '1', null, null);
45:
46: 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)
47: values ('4028e4d35b5ca4ee015b6139c1dc2fb4', '七里河站', '110002012', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:13:46', '402881e54c4