oracle 树结构的查询总结
来源:互联网 发布:java客户端服务端通信 编辑:程序博客网 时间:2024/05/18 01:53
select t.*, t.rowid from SYS_DEPT_V t
--lpad函数和伪列level模拟树形结构
select LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE
from SYS_DEPT_V s
start with s.parent_dept_id = '-1'
connect by prior s.id = s.parent_dept_id;
--start with 和 connect by
select s.*, level from SYS_DEPT_V s start with s.parent_dept_id = '-1'
connect by prior s.id = s.parent_dept_id;
--prior 递归方向(上和下)
select s.*, level from SYS_DEPT_V s start with s.id = '402880484105b3b101410a8bdef00096'
connect by s.id = prior s.parent_dept_id;
--排除树节点(条件与start with 一致)
select s.id, s.parent_dept_id, LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE, s.order_by, level
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id and s.id <> '402880484105b3b101410a8ada52008e';
--排除节点(条件与start with 一致)
select s.id, s.parent_dept_id, LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE, s.order_by, level
from SYS_DEPT_V s
where s.id <> '402880484105b3b101410a8ada52008e'
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id;
--树排序1(order siblings by同部门排序)
select s.id, s.parent_dept_id, LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE, s.order_by, level
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id and s.id <> '402880484105b3b101410a8ada52008e' order siblings by s.dept_code;
--树排序2(递归后整体排序,树形结构打乱)
select s.id, s.parent_dept_id, LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE, s.order_by, level
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id and s.id <> '402880484105b3b101410a8ada52008e' order by s.dept_code;
--叶子节点(connect_by_isleaf)
select LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE,
CONNECT_BY_ISLEAF leaf
from SYS_DEPT_V s
start with s.parent_dept_id = '-1'
connect by prior s.id = s.parent_dept_id;
--根节点(connect_by_root)
select LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE,
CONNECT_BY_ISLEAF leaf,
CONNECT_BY_ROOT s.dept_code root
from SYS_DEPT_V s
start with s.parent_dept_id = '-1'
connect by prior s.id = s.parent_dept_id;
--树的路径(SYS_CONNECT_BY_PATH)
select LPAD(' ', 5 * (LEVEL - 1)) || s.DEPT_CODE DEPT_CODE,
CONNECT_BY_ISLEAF leaf,
CONNECT_BY_ROOT s.dept_code root,
SYS_CONNECT_BY_PATH(s.dept_code,'/') "path"
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id;
--树的路径(wmsys.wm_concat()默认以分号隔离)
select wm_concat(s.id) treepath
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id;
--树的路径(很奇异)
select wmsys.wm_concat(s.id)over(order by s.id) treepath
from SYS_DEPT_V s
start with s.id = '402880484105b3b101410a8ada52008e'
connect by prior s.id = s.parent_dept_id;
--over(分组和排序) row_number()
--父节点下所有子节点的值
select parent_dept_id,
ltrim(sys_connect_by_path(id, '*'), '*') "values"
from (select t.*,
(row_number() /* 按照parentid分组,组内部按照dataid排序后的行号*/
over(partition by t.parent_dept_id order by t.id)) numid
from SYS_DEPT_V t )
WHERE connect_by_isleaf = 1
start with numid = 1
connect by numid - 1 = prior numid
and parent_dept_id = prior parent_dept_id;
- oracle 树结构的查询总结
- ORACLE - 树结构查询
- Oracle树结构查询
- Oracle 树结构查询
- oracle 树结构查询
- oracle 树结构查询
- Oracle 树结构查询
- oracle树结构查询
- Oracle树结构查询
- oracle的树型结构查询
- Oracle中树结构的查询
- Oracle 中树结构查询
- Oracle树结构查询[转载]
- Oracle 树型结构查询
- Oracle树结构查询[转载]
- Oracle树结构表查询
- oracle树结构节点查询
- Oracle查询树结构表
- 美团点评笔试题
- mybatis自动生成器(mybatis.generator)使用
- 超级楼梯
- C#进阶 02 注释符
- qt线程通信(通过信号触发来传递参数)
- oracle 树结构的查询总结
- 算法系列(二)冒泡排序、选择排序、插入排序和希尔排序(Java实现)
- 4Sum II问题及解法
- Nginx 之一:编译安装nginx 1.8.1 及配置
- c++基础之位操作符
- Unity入门操作_动画系统_Mecanim_032
- cordova热更新插件的使用
- Mysql数据备份、恢复、数据导入、数据导出的方法
- 英语语法结构体系