博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
connect by和strart with子句
阅读量:5810 次
发布时间:2019-06-18

本文共 1626 字,大约阅读时间需要 5 分钟。

--使用connect by和strart with子句

SELECT [level],column,expression, ...
FROM table
[WHERE where_clause]
[[START WITH start_condition] [CONNECT BY prior_condition]];

SELECT  empno,

           mgr,
           ename,
           job
      FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr;

 
--使用偽列level
SELECT LEVEL,
           empno,
           mgr,
           ename,
           job
      FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
  ORDER BY LEVEL, mgr, empno;
 

--格式化層次查詢的結果
SELECT LEVEL,
           empno,
           mgr,
          lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
START WITH mgr is null
CONNECT BY PRIOR empno = mgr;

--從非根節點開始遍歷

SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
START WITH ename like 'JONES'
CONNECT BY PRIOR empno = mgr;

--在start with子句中使用子查詢

  SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
START WITH empno=(select empno from emp where ename='CLARK')
CONNECT BY PRIOR empno = mgr;

--向上遍歷

SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
START WITH ename like 'JONES'
CONNECT BY PRIOR mgr=empno;

--從層次化查詢中刪除節點
SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
WHERE ename !='JONES'
START WITH mgr is null
CONNECT BY PRIOR empno = mgr;

--上面的查詢雖然刪除了節點但是並沒有刪除該節點的分支,為了連同分支也一併刪除,在connect by子句中使用and子句

SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job
      FROM emp
START WITH mgr is null
CONNECT BY PRIOR empno = mgr
AND  ename !='JONES';

--在層次化查詢中加入其它條件

SELECT LEVEL,
           empno,
           mgr,
           lpad(' ',2*level-1)||ename as employee,
           job,
           sal
      FROM emp
WHERE sal<3000     
START WITH mgr is null
CONNECT BY PRIOR empno = mgr;

转载于:https://www.cnblogs.com/guilingyang/p/6367613.html

你可能感兴趣的文章
灰度图像和彩色图像
查看>>
通过vb.net 和NPOI实现对excel的读操作
查看>>
TCP segmentation offload
查看>>
java数据类型
查看>>
数据结构——串的朴素模式和KMP匹配算法
查看>>
FreeMarker-Built-ins for strings
查看>>
验证DataGridView控件的数据输入
查看>>
POJ1033
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
一维数组
查看>>
Linux学习笔记之三
查看>>
Floyd最短路算法
查看>>
Class.forName(String name)方法,到底会触发那个类加载器进行类加载行为?
查看>>
CentOS 6.6 FTP install
查看>>
C#------判断btye[]是否为空
查看>>
图解Ajax工作原理
查看>>
oracle导入导出小记
查看>>
聊一聊log4j2配置文件log4j2.xml
查看>>
NeHe OpenGL教程 第七课:光照和键盘
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>