欢迎投稿

今日深度:

Oracle树形结构查询(递归),树形结构递归

Oracle树形结构查询(递归),树形结构递归


引用: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', '402881e54c40d74d014c40d8407a0016', '1', 148, '1', null, null);
  48:? 
  49: 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)
  50: values ('4028e4d35b5ca4ee015b613a24853047', '西站十字站', '110002013', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:14:12', '402881e54c40d74d014c40d8407a0016', '1', 149, '1', null, null);
  51:? 
  52: 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)
  53: values ('4028e4d35b5ca4ee015b613a81f030ce', '兰州西站北广场站', '110002014', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:14:36', '402881e54c40d74d014c40d8407a0016', '1', 150, '1', null, null);
  54:? 
  55: 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)
  56: values ('4028e4d35b5ca4ee015b613ad627313d', '土门墩站', '110002015', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:14:57', '402881e54c40d74d014c40d8407a0016', '1', 151, '1', null, null);
  57:? 
  58: 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)
  59: values ('4028e4d35b5ca4ee015b613b394c31c6', '马滩站', '110002016', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:15:22', '402881e54c40d74d014c40d8407a0016', '1', 152, '1', null, null);
  60:? 
  61: 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)
  62: values ('4028e4d35b5ca4ee015b613b9051325e', '兰州海关站', '110002017', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:15:45', '402881e54c40d74d014c40d8407a0016', '1', 153, '1', null, null);
  63:? 
  64: 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)
  65: values ('4028e4d35b5ca4ee015b613c286b332e', '兰州城市学院(省科技馆)站', '110002018', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:16:24', '402881e54c40d74d014c40d8407a0016', '1', 154, '1', null, null);
  66:? 
  67: 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)
  68: values ('4028e4d35b5ca4ee015b613c806933a3', '深安大桥南站', '110002019', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:16:46', '402881e54c40d74d014c40d8407a0016', '1', 155, '1', null, null);
  69:? 
  70: 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)
  71: values ('4028e4d35b5ca4ee015b613cdf98342c', '陈官营站', '110002020', '4028e4d35b5ca4ee015b60f9fae95a44', null, null, '2017-04-12 16:17:11', '402881e54c40d74d014c40d8407a0016', '1', 157, '1', null, null);
  72:? 
  73: 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)
  74: values ('4028e4d35b5ca4ee015b60fa3e2f5a94', '乘务中心', '110003', '40288ac45a3c1e8b015a3c28b4ae01d6', null, null, '2017-04-12 15:04:24', '402881e54c40d74d014c40d8407a0016', '1', 65, '1', null, null);
  75:? 
  76: 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)
  77: values ('4028e4d35b5ca4ee015b613d738d34f4', '陈官营车场组', '110003001', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:17:48', '402881e54c40d74d014c40d8407a0016', '1', 158, '1', null, null);
  78:? 
  79: 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)
  80: values ('4028e4d35b5ca4ee015b613defed359e', '东岗车场组', '110003002', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:18:20', '402881e54c40d74d014c40d8407a0016', '1', 159, '1', null, null);
  81:? 
  82: 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)
  83: values ('4028e4d35b5ca4ee015b613e42ae3612', '第一车队', '110003003', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:18:41', '402881e54c40d74d014c40d8407a0016', '1', 161, '1', null, null);
  84:? 
  85: 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)
  86: values ('4028e4d35b5ca4ee015b613e7a50366c', '第二车队', '110003004', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:18:56', '402881e54c40d74d014c40d8407a0016', '1', 162, '1', null, null);
  87:? 
  88: 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)
  89: values ('4028e4d35b5ca4ee015b613ebc8e36c1', '第三车队', '110003005', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:19:13', '402881e54c40d74d014c40d8407a0016', '1', 163, '1', null, null);
  90:? 
  91: 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)
  92: values ('4028e4d35b5ca4ee015b613eff483729', '第四车队', '110003006', '4028e4d35b5ca4ee015b60fa3e2f5a94', null, null, '2017-04-12 16:19:30', '402881e54c40d74d014c40d8407a0016', '1', 164, '1', null, null);

  在这张表中有三个字段:dept_id 部门主键id;dept_name? 部门名称;dept_code 部门编码;par_dept_id?? 父级部门id(首级部门为 -1);

?

下面以最简单的情况进行示例说明:

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;

说明:其中的f_id为标识码,f_pid为父节点标识码,f_name为名称

www.htsjk.Com true http://www.htsjk.com/oracle/35773.html NewsArticle Oracle树形结构查询(递归),树形结构递归 引用:https://blog.csdn.net/u012615705/article/details/78321022 文章转自上述地址,内部有稍许改动,如有需要请查看原文。 oracle树状结构查询即层次递...
相关文章
    暂无相关文章
评论暂时关闭