在做项目中,我们经常会遇到树形数据,在oracle树查询的最重要的就是select…start with…connect by…prior语法了。根据该语法,我们可以将一个表形结构的以树的顺序列出来。
SQL脚本用来创建表、序列、索引、测试数据等、
drop table t_dept; drop sequence seq_dept; drop index t_index_parent_id; drop index t_index_dept_code;
--创建表; create table t_dept ( deptid number not null primary key, dentname_en varchar2(200) not null, deptname_cn varchar2(200) not null, deptcode varchar2(200) not null, parent_id number(10) default -1);
--创建序列; create sequence seq_dept start with 100 increment by 1 minvalue 1 maxvalue 1000000 nocycle cache 20;
--创建索引; create index t_index_parent_id on scott.t_dept(parent_id); create index t_index_dept_code on t_dept(deptcode);
--插入测试数据; insert into scott.t_dept values(seq_dept.nextval,'Front Office Department','前台部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Financial Department','财务部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Logistics Department','后勤部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Recruiting Department','招聘部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Development department','开发部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Dngineering department','工程部','0'||seq_dept.nextval,-1); insert into scott.t_dept values(seq_dept.nextval,'Front Office Department_001','前台接待分部','0'||seq_dept.nextval,100); insert into scott.t_dept values(seq_dept.nextval,'Front Office Department_002','前台分流部','0'||seq_dept.nextval,100); insert into scott.t_dept values(seq_dept.nextval,'Front Office Department_003','前台引导客流部','0'||seq_dept.nextval,100); insert into scott.t_dept values(seq_dept.nextval,'Receptionist Department_001','前台接待普通客户部','0'||seq_dept.nextval,106); insert into scott.t_dept values(seq_dept.nextval,'Receptionist Department_002','前台接待中级客户部','0'||seq_dept.nextval,106); insert into scott.t_dept values(seq_dept.nextval,'Receptionist Department_003','前台接待高级客户部','0'||seq_dept.nextval,106); insert into scott.t_dept values(seq_dept.nextval,'Sweep the toilet Department','扫厕所分部','0'||seq_dept.nextval,102); insert into scott.t_dept values(seq_dept.nextval,'Logistical support Department','后勤保障部','0'||seq_dept.nextval,102); insert into scott.t_dept values(seq_dept.nextval,'Clean the health','打扫卫生分部','0'||seq_dept.nextval,102); insert into scott.t_dept values(seq_dept.nextval,'Men lavatory men toilet','打扫男厕所分部','0'||seq_dept.nextval,112); insert into scott.t_dept values(seq_dept.nextval,'Women lavatory','打扫女厕所分部','0'||seq_dept.nextval,112); insert into scott.t_dept values(seq_dept.nextval,'PHP Development department','PHP开发部','0'||seq_dept.nextval,104); insert into scott.t_dept values(seq_dept.nextval,'.Net Development department','.NET开发部','0'||seq_dept.nextval,104); insert into scott.t_dept values(seq_dept.nextval,'Java Development department','Java开发部','0'||seq_dept.nextval,104); insert into scott.t_dept values(seq_dept.nextval,'C++ Development department','C++开发部','0'||seq_dept.nextval,104); insert into scott.t_dept values(seq_dept.nextval,'Python Development department','Python开发部','0'||seq_dept.nextval,104); insert into scott.t_dept values(seq_dept.nextval,'Java Development department_001','Java前台开发部','0'||seq_dept.nextval,123); insert into scott.t_dept values(seq_dept.nextval,'Java Development department_002','Java后台开发部','0'||seq_dept.nextval,123); commit;
--所有t_dept表中的数据结果为:
--根据子部门查询出父部门(当prior parent = id时,数据库会跟据当前的parent来迭代出与当前的parent相同的id的记录,所以查询出来的结果就是所有的父类结果)。 select deptid, dentname_en, deptname_cn, deptcode, parent_id, level from scott.t_dept start with deptid = 109 connect by prior parent_id = deptid; 等效于: --根据子部门查询出父部门 select deptid, dentname_en, deptname_cn, deptcode, parent_id, level from scott.t_dept start with deptid = 109 connect by deptid = prior parent_id;
查询结果:
--根据父部门查询出子部门(当parent = prior id时,数据库会根据当前的id迭代出parent与该id相同的记录,所以查询的结果是迭代出了所有的子类记录); select deptid, dentname_en, deptname_cn, deptcode, parent_id, level from scott.t_dept start with deptid = 100 connect by parent_id = prior deptid ; 等效于: --根据父部门查询出子部门 select deptid,dentname_en,deptname_cn,deptcode,parent_id,level from scott.t_dept dept start with deptid = 100 connect by prior dept.deptid = dept.parent_id;
查询结果:
--根据当前子部门查询出顶级部门ID以及部门Name:(oracle11g里面可以采用connect_by_root,非常的方便) select * from t_dept d where d.deptid = (select distinct FIRST_VALUE(deptid) OVER(ORDER BY LEVEL DESC ROWS UNBOUNDED PRECEDING) AS firstdeptid from t_dept start with deptid = 109 connect by prior parent_id = deptid);
查询 结果:
相关推荐
Oracle start with.connect by prior子句实现递归查询
oracle中 connect by prior 递归算法 Oracle中start with...connect by prior子句用法 connect by 是结构化查询中用到的
在 Oracle 中是通过 start with connect by prior 语法来实现递归查询的。 按照 prior 关键字在子节点端还是父节点端,以及是否包含当前查询的节点,共分为四种情况。 prior 在子节点端(向下递归) 第一种情况: ...
主要给大家介绍了关于Oracle递归查询start with connect by prior、的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
递归求全排列.rar 递归求全排列.rar 递归求全排列.rar 递归求全排列.rar 递归求全排列.rar 递归求全排列.rar 递归求全排列.rar
在Oracle中用Start with...Connect By子句递归查询
易语言源码递归算法2.rar 易语言源码递归算法2.rar 易语言源码递归算法2.rar 易语言源码递归算法2.rar 易语言源码递归算法2.rar 易语言源码递归算法2.rar
Oracle使用递归查询。查询树结构的sql。在Oracle中,递归查询要用到start with ……connect by prior……
rls递归最小二乘.pdfrls递归最小二乘.pdfrls递归最小二乘.pdfrls递归最小二乘.pdfrls递归最小二乘.pdfrls递归最小二乘.pdf
易语言源码递归算法1.rar 易语言源码递归算法1.rar 易语言源码递归算法1.rar 易语言源码递归算法1.rar 易语言源码递归算法1.rar 易语言源码递归算法1.rar
易语言源码易语言无递归遍历源码.rar 易语言源码易语言无递归遍历源码.rar 易语言源码易语言无递归遍历源码.rar 易语言源码易语言无递归遍历源码.rar 易语言源码易语言无递归遍历源码.rar 易语言源码易语言无...
java递归之return.docxjava递归之return.docx java递归之return.docxjava递归之return.docx java递归之return.docxjava递归之return.docx
非递归汉洛塔.c非递归汉洛塔.c非递归汉洛塔.c
C语言中递归的探讨.pdf
18.递归算法与递归算法应用.ppt
n后问题--非递归迭代回溯.rar n后问题--非递归迭代回溯.rar n后问题--非递归迭代回溯.rar n后问题--非递归迭代回溯.rar n后问题--非递归迭代回溯.rar n后问题--非递归迭代回溯.rar
3.6.11 我可以在任意版本的Oracle中使用任意版本的预编译器么?.................................10 3.6.12 1405 错误(Fetch column values is null)可避免么?..................................................
C递归遍历目录.txt 适合初学者使用.。。。。。。。。。
oracle递归查询