<table>
<tbody>
<tr>
<td>
<div class="xiantao ">
</div>
</td>
</tr>
<tr>
<td style="height: 200px;
" vAlign="top " width="670 " align="left ">
<span id="articlecontent " onmouseup="NewHighlight(event)" onmousedown="newhighlight = true;
">
<table>
<tbody>
<tr>
<td style="position: relative;
" width="670 ">
<h1 style="color: rgb(75, 75, 75);
font - family: Verdana,
Geneva,
Arial,
Helvetica,
sans - serif;
font - size: 14.7px;
margin - bottom: 10px;
" class="postTitle ">
<a style="color: rgb(26, 139, 200);
text - decoration: none;
" id="cb_post_title_url " class="postTitle2 " href="http: //www.cnblogs.com/caroline/archive/2011/12/25/2301083.html">
ORACLE--Connect By、Level、Start With的使用(Hierarchical query-层次查询)
</a>
</h1>
<div style="color: rgb(75, 75, 75); line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; word-break: normal !important;"
id="cnblogs_post_body">
<table style="border-style: solid; border-color: rgb(192, 192, 192); width: 1026px;"
border="1" cellSpacing="1" cellPadding="2" bgColor="#d8d8c4">
<tbody>
<tr>
<td style="border-style: solid; border-color: rgb(192, 192, 192); padding: 3px; color: rgb(69, 69, 69); font-size: 12px; word-break: normal !important;">
<span style="line-height: 18px; font-family: Arial; font-size: x-small;">
Syntax 1
</span>
</td>
<td style="border-style: solid; border-color: rgb(192, 192, 192); padding: 3px; color: rgb(69, 69, 69); font-size: 12px; word-break: normal !important;">
<span style="line-height: 18px; font-family: Courier;">
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/CONNECT_BY.htm">
CONNECT BY
</a>
[NOCYCLE] <condition>
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/START_WITH.htm">
START WITH
</a>
<condition>
</span>
</td>
</tr>
<tr>
<td style="border-style: solid; border-color: rgb(192, 192, 192); padding: 3px; color: rgb(69, 69, 69); font-size: 12px; word-break: normal !important;">
<span style="line-height: 18px; font-family: Arial; font-size: x-small;">
Syntax 2
</span>
</td>
<td style="border-style: solid; border-color: rgb(192, 192, 192); padding: 3px; color: rgb(69, 69, 69); font-size: 12px; word-break: normal !important;">
<span style="line-height: 18px; font-family: Courier;">
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/START_WITH.htm">
START WITH
</a>
<condition>
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/CONNECT_BY.htm">
CONNECT BY
</a>
[NOCYCLE] <condition>
</span>
</td>
</tr>
</tbody>
</table>
<p style="margin: 10px auto;">
参考网址:
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:489772591421">
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:489772591421
</a>
</p>
<p style="margin: 10px auto;">
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/reference/connectby.html">
http://psoug.org/reference/connectby.html
</a>
</p>
<p style="margin: 10px auto;">
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://www.oradev.com/connect_by.jsp">
http://www.oradev.com/connect_by.jsp
</a>
</p>
<p style="margin: 10px auto;">
<a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://philip.greenspun.com/sql/trees.html">
http://philip.greenspun.com/sql/trees.html
</a>
<br>
查找员工编号为7369的领导:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
,E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
EMP E CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.MGR
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
E.EMPNO START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7876
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
ORDER
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
DESC
</span>
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_1.jpg">
</p>
<p style="margin: 10px auto;">
"
<span style="color: rgb(255, 0, 0);">
start with
</span>
" -- this identifies all LEVEL=1 nodes in the tree
</p>
<p style="margin: 10px auto;">
"
<span style="color: rgb(255, 0, 0);">
connect by
</span>
" -- describes how to walk from the parent nodes above to their children
and
<br>
their childrens children.
</p>
<p style="margin: 10px auto;">
Easiest to use an example on emp. If we start with "where mgr is NULL",
we generate the
<br>
set of employees that have no mgr (they are the top of the tree). If we
</p>
<p style="margin: 10px auto;">
<span style="color: rgb(255, 0, 0);">
CONNECT BY PRIOR EMPNO = /* current */ MGR
</span>
</p>
<p style="margin: 10px auto;">
that will take all of the PRIOR records (the start with at first) and
find all records
<br>
such that the MGR column equals their EMPNO (find all the records of people
managed by
<br>
the people we started with).
</p>
<p style="margin: 10px auto;">
<br>
使用WITH语句优化查询结果:
<span style="color: rgb(255, 0, 0);">
优化等级
</span>
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
A
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
(
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(255, 0, 255); line-height: 1.5;">
MAX
</span>
(
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
+
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
</span>
LVL
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
EMP E
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.MGR
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
E.EMPNO
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7876
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
ORDER
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
DESC
</span>
)
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
A.LVL 最高等级加1,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
当前等级,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
9
</span>
A.LVL
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
优化后等级,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
10
</span>
E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
A,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
11
</span>
EMP E CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.MGR
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
E.EMPNO START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7876
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
ORDER
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
DESC
</span>
</pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_3.jpg">
</p>
<p style="margin: 10px auto;">
查找员工编号为7839的所有下属(7839为king):
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
等级, E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
EMP E
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
E.MGR
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7839
</span>
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_4.jpg">
</p>
<p style="margin: 10px auto;">
--构造整个的层次结构
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
select
</span>
lpad(
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
level
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
</span>
,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
ename ename, empno, mgr
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
from
</span>
emp
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
MGR
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
MGR
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_5.jpg">
</p>
<p style="margin: 10px auto;">
So, KING is the start with set then JONES BLAKE and CLARK fall under him.
Each of them
<br>
becomes the
<span style="color: rgb(255, 0, 0);">
PRIOR record in turn and their trees are expanded.
</span>
</p>
<p style="margin: 10px auto;">
<br>
<br>
</p>
<p style="margin: 10px auto;">
使用Connect By 结合 level构造虚拟行:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
DUAL CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
<
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
5
</span>
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_6.jpg">
</p>
<p style="margin: 10px auto;">
使用rownum实现类似的功能:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
DUAL CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
<
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
5
</span>
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_7.jpg">
</p>
<p style="margin: 10px auto;">
---------------------待续-----------------------
</p>
<p style="margin: 10px auto;">
使用UNION ALL构造两层节点的树:
</p>
<p style="margin: 10px auto;">
视图如下所示:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
CREATE
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
OR
</span>
<span style="color: rgb(255, 0, 255); line-height: 1.5;">
REPLACE
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
VIEW
</span>
TREE_VIEW
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
1
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
rootnodeid,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
xxxx有限责任公司
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
treename,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
-1
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
parent_id
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
dual
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
UNION
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
9
</span>
to_char(d.deptno),
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
10
</span>
d.dname
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
_
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
d.loc,
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
11
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
1
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
parent_id
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
12
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
dept d;
</pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;">
查询语句:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
T.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
TREE_VIEW T
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
T.PARENT_ID
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
-1
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<br>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR T.ROOTNODEID
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
T.PARENT_ID
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_8.png">
</p>
<p style="margin: 10px auto;">
<span style="color: rgb(255, 102, 0);">
-----以下为更新内容:
</span>
</p>
<p style="margin: 10px auto;">
1、先查看总共有几个等级:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(255, 0, 255); line-height: 1.5;">
COUNT
</span>
(
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
)
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
< /span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
;
< /pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_9.png">
</p>
<p style="margin: 10px auto;">
2、查看每个等级的人数。主要是通过LEVEL进行GROUP BY
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(255, 0, 255); line-height: 1.5;">
COUNT
< /span>
(
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
)
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
< /span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
GROUP
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
;
< /pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_10.png">
</p>
<p style="margin: 10px auto;">
3、Oracle 10g提供了一个简单的connect_by_isleaf=1,
</p>
<p style="margin: 10px auto;">
0表示非叶子节点
< /p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
< /span>
等级, CONNECT_BY_ISLEAF
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
是否是叶子节点, E.
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
< /span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_11.png">
</p>
<p style="margin: 10px auto;">
4、SYS_CONNECT_BY_PATH
</p>
<p style="margin: 10px auto;">
Oracle 9i提供了sys_connect_by_path(column, char), 其中column是字符型或能自动转
< /p>
<p style="margin: 10px auto;">
换成字符型的列名。它的主要目的就是将父节点到当前节点的”path”按照指定的模式展现出现。这个函数只能使用在层次查询中。
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
等级,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
CONNECT_BY_ISLEAF
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
是否是叶子节点,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
< /span>
LPAD(
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
</span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
SYS_CONNECT_BY_PATH(ENAME,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
=>
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
)
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
;
</pre>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_12.jpg">
</p>
<p style="margin: 10px auto;">
5、修剪树枝和节点:
</p>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_13.png">
</p>
<p style="margin: 10px auto;" align="left">
过滤掉编号是7566的数据(修剪节点),他指的是把这个节点给裁掉,但是并没有破坏树结构,它的子节点还是可以正常的显示。
</p>
<div style='margin: 5px 0px;
padding: 5px;
border: 1px solid rgb(204, 204, 204);
overflow: auto;
font - family: "Courier New";
font - size: 12px;
background - color: rgb(245, 245, 245);
' class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font - family: "Courier New";
margin - top: 0px;
margin - bottom: 0px;
white - space: pre - wrap;
word - wrap: break - word;
'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
等级,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
CONNECT_BY_ISLEAF
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
是否是叶子节点,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
LPAD(
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
< /span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
< /span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
< /span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
SYS_CONNECT_BY_PATH(ENAME,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
=>
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
),
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WHERE
< /span>
e.empno
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
!=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7566
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
</span>
CONNECT
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
< /span>
PRIOR E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
</span>
START
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
< /span>
E.MGR
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
< /span>
;
</pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_14.jpg">
</p>
<p style="margin: 10px auto;">
裁掉编号是7698的节点和它的子节点:
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
等级,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
< /span>
CONNECT_BY_ISLEAF
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
是否是叶子节点,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
LPAD(
< span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
</span>
)
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
SYS_CONNECT_BY_PATH(ENAME,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
= >
< /span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
),
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
</span>
E.
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
< /span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
< /span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
AND
</span>
E.EMPNO
< span style="color: rgb(128, 128, 128); line-height: 1.5;">
!=
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
7698
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
< /span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
;
< /pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http:/ / image60.360doc.com / DownloadImg / 2013 / 04 / 2216 / 31821006_2.gif ">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;
">
<img style="border: 0px currentColor;
" alt="" src="http: //image60.360doc.com/DownloadImg/2013/04/2216/31821006_15.png">
</p>
<p style="margin: 10px auto;">
6、CONNECT_BY_ROOT的使用,oracle10g新增connect_by_root,用在列名之前表示此行的根节点的相同列名的值。
</p>
<div style='margin: 5px 0px; padding: 5px; border: 1px solid rgb(204, 204, 204); overflow: auto; font-family: "Courier New"; font-size: 12px; background-color: rgb(245, 245, 245);'
class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font-family: "Courier New"; margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
等级,
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
CONNECT_BY_ISLEAF
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
是否是叶子节点,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
CONNECT_BY_ROOT ENAME,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
LPAD(
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
</span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
</span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
SYS_CONNECT_BY_PATH(ENAME,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
=>
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
),
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
</span>
E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
</span>
CONNECT
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
PRIOR E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
</span>
START
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
</span>
E.MGR
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
</span>
;
</pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;">
</p>
<p style="margin: 10px auto;">
<img style="border: 0px currentColor;" alt="" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_16.png">
</p>
<p style="margin: 10px auto;" align="left">
对于层次查询如果用order by排序,比如order by last_name则是先做完层次获得level,然后按last_name 排序,这样破坏了层次,比如特别关注某行的深度,按level 排序,也是会破坏层次的。在oracle10g中,增加了siblings
关键字的排序。
</p>
<p style="margin: 10px auto;" align="left">
语法:order siblings by <expre>
</p>
<p style="margin: 10px auto;">
它会保护层次,并且在每个等级中按expre排序。
</p>
<div style='margin: 5px 0px;
padding: 5px;
border: 1px solid rgb(204, 204, 204);
overflow: auto;
font - family: "Courier New";
font - size: 12px;
background - color: rgb(245, 245, 245);
' class="cnblogs_code">
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http://image60.360doc.com/DownloadImg/2013/04/2216/31821006_2.gif">
</a>
</span>
</div>
<pre style='font - family: "Courier New";
margin - top: 0px;
margin - bottom: 0px;
white - space: pre - wrap;
word - wrap: break - word;
'>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
1
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
SELECT
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
等级,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
2
</span>
CONNECT_BY_ISLEAF
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
AS
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
是否是叶子节点,
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
3
</span>
LPAD(
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
,
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
LEVEL
< /span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
2
< /span>
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
-
</span>
<span style="color: rgb(128, 0, 0); line-height: 1.5; font-weight: bold;">
1
< /span>
)
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
||
</span>
SYS_CONNECT_BY_PATH(ENAME,
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
=>
</span>
<span style="color: rgb(255, 0, 0); line-height: 1.5;">
'
< /span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
),
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
4
< /span>
E.
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
*
</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
5
< /span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
FROM
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
EMP E
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
6
</span>
CONNECT
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
< /span>
PRIOR E.EMPNO
<span style="color: rgb(128, 128, 128); line-height: 1.5;">
=
</span>
<span style="color: rgb(0, 0, 0); line-height: 1.5;">
E.MGR
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
7
</span>
START
< span style="color: rgb(0, 0, 255); line-height: 1.5;">
WITH
< /span>
E.MGR
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
IS
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
NULL
< /span>
<span style="color: rgb(0, 128, 128); line-height: 1.5;">
8
</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
ORDER
< /span>
SIBLINGS
<span style="color: rgb(0, 0, 255); line-height: 1.5;">
BY
</span>
E.ENAME;
< /pre>
<div style="margin-top: 5px;" class="cnblogs_code_toolbar">
<span style="line-height: 1.5; padding-right: 5px;" class="cnblogs_code_copy">
<a style="border: currentColor !important; color: rgb(26, 139, 200); text-decoration: none;"
title="复制代码" href="">
<img style="border: currentColor !important;" alt="复制代码" src="http:/ / image60.360doc.com / DownloadImg / 2013 / 04 / 2216 / 31821006_2.gif ">
</a>
</span>
</div>
</div>
<p style="margin: 10px auto;
">
<img style="border: 0px currentColor;
" alt="" src="http: //image60.360doc.com/DownloadImg/2013/04/2216/31821006_17.png">
</p>
<h3 style="margin: 10px 0px; color: rgb(102, 102, 102); font-family: Verdana; font-size: 14px; background-image: none; background-repeat: no-repeat no-repeat;">
<span style="color: rgb(255, 0, 0); line-height: 21px;">
connect_by_iscycle(存在循环,将返回1,否则返回0)
</span>
</h3>
<p style="margin: 10px auto;">
The CONNECT_BY_ISCYCLE pseudocolumn returns 1 if the current row has a
child which is also its ancestor. Otherwise it returns 0.
<br>
You can specify CONNECT_BY_ISCYCLE only if you have specified the NOCYCLE
parameter of the CONNECT BY clause. NOCYCLE enables Oracle to return the
results of a query that would otherwise fail because of a CONNECT BY loop
in the data.
</p>
</div>
<div style="color: rgb(75, 75, 75); line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; margin-top: 10px;"
id="MySignature">
I believe that we are who we choose to be. Nobody‘s going to come and
save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
</div>
< /td>
</tr>
</tbody>
</table>
<div style="width: 717px; height: 700px; display: none;" id="viewerPlaceHolder">
</div>
</span>
<table>
<tbody>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="left">
<div style="height: 28px;">
<div style="color: rgb(121, 123, 122); font-size: 14px; float: left;">
来自:
< span class="name" onclick="wzhitnew(59)">
<a href="http://www.360doc.com/userhome/11947209" target="_blank">
风中的眼睛_
< /a>
</span>
& gt; & nbsp;
< span id="cname" onclick="wzhitnew(60)">
<a href="http://www.360doc.com/userhome.aspx?userid=11947209&cid=3"
target="_blank">
《数据库函数》
< /a>
</span>
</div>
<div style="float: left;">
<div id="spanfollowstatus1" class="btzctsg">
<div class="gzbt1">
<a onclick="gzuser(11947209);wzhitnew(61);" href="javascript:void(0)">
</a>
</div>
</div>
<script>
isfollow(11947209); < /script>
</div > </div>
<div>
<div style="padding-top: 0px;" class="modlist lf360 list">
<ul>
<div id="lastart">
</div > <div id = "nextart" > 下一篇: < a onclick = "wzhitnew(58);"href = "http://www.360doc.com/content/13/0426/15/11947209_281077725.shtml"target = "_blank" > Oracle中rownum与rowid的理解 < /a></div > </ul>
</div > </div>
</td > </tr>
<tr>
<td align="left">
<div style="padding-top: 12px;">
<script type="text/javascript ">
/*文章页正文下方标签云,2013-8-21*/
var cpro_id = "u1350774 ";
"
</script>
<script type="text / javascript " src="http: //cpro.baidustatic.com/cpro/ui/c.js">
</script>
<div id="BAIDU_DUP_wrapper_u1350774_0">
<iframe height="120" marginHeight="0" src="http://pos.baidu.com/ecom?cec=utf-8&dai=1&cfv=12&cpa=1&col=zh-cn&dis=0&xuanting=0&n=360doc168_cpr&conOP=0&scale=&skin=tabcloud_skin_1&rsi0=650&rsi1=120&rsi5=4&ltr=&ltu=http%3A%2F%2Fwww.360doc.com%2Fcontent%2F13%2F0422%2F16%2F11947209_280153192.shtml&pcs=1423x761&rss0=&rss1=&rss2=&rss3=&rss4=&rss5=&rss6=&rss7=&rad=&pis=10000x10000&aurl=&psr=1440x900&pss=1423x7845&stid=5&tpr=1395824780709&lunum=6&ch=0&at=103&qn=b09f911f039c88e8&ps=7845x239&tn=baiduCustSTagLinkUnit&ts=1&c01=0&td_id=1350774&adn=0&cad=1&ccd=24&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1350774"
frameBorder="0" width="650" allowTransparency="true" marginWidth="0" scrolling="no"
align="center,center">
</iframe>
</div>
<script charset="utf-8" src="http://pos.baidu.com/ecom?di=u1350774&dcb=BAIDU_DUP2_define&dtm=BAIDU_DUP2_SETJSONADSLOT&dbv=0&dci=0&dri=0&dis=0&dai=1&dds=&drs=3&dvi=1395372013&ltu=http%3A%2F%2Fwww.360doc.com%2Fcontent%2F13%2F0422%2F16%2F11947209_280153192.shtml&liu=&ltr=&lcr=&ps=7845x239&psr=1440x900&par=1440x860&pcs=1423x761&pss=1423x7845&pis=-1x-1&cfv=12&ccd=24&chi=1&cja=true&cpl=0&cmi=0&cce=true&col=zh-cn&cec=utf-8&cdo=-1&tsr=560&tlm=1395824780&tcn=1395824781&tpr=1395824780709&dpt=none&coa=c01%3D0&baidu_id=">
</script>
< /div>
</td>
</tr>
<tr>
<td style="text-align: left;">
<div style="width: 670px; padding-top: 15px;">
<table style="width: 670px;" cellSpacing="0" cellPadding="0">
<tbody>
<tr>
<td width="157">
</td>
<td width="314">
<div style="cursor: pointer;" class=" lf360">
<div class="zscbt">
<a onclick="SaveArt();wzhitnew(26);" href="javascript:void(0);">
</a>
</div>
</div>
<div style="margin-left: 10px; cursor: pointer;" id="sendflowerdiv" class="lf360 xhmainbox"
onmouseover="ChangeFlowerBtnStyle(1);" onmouseout="ChangeFlowerBtnStyle(2);"
onclick='Showflowerlayer("sendedLayer1");wzhitnew(27)'>
<div id="flowimg2" class="lf360">
<div style="padding-top: 12px; padding-left: 16px;">
<img id="flowimg3" src="http:/ / pubimage.360doc.com / wz / huahua.gif ">
</div>
</div>
<div id="flowimg5 " class="lf360 xianhua ">
献花(
<span id="articleflowernum ">
0
</span>
)
<div id="flowernumadd " class="addtionone ">
+1
</div>
</div>
</div>
</td>
<td style="font - size: 12px; vertical - align: bottom;
" width="197 ">
<div id="sharediv2 " class="rt360 ">
<table style="vertical - align: text - bottom;
" border="0 " cellSpacing="0 " cellPadding="0 ">
<tbody>
<tr>
<td>
<div style="width: 126px; height: 20px;
">
<div style="float: left;
">
<span style="color: rgb(149, 149, 149); line - height: 16px; font - size: 12px;
">
分享:
</span>
</div>
<div style="width: 22px; float: left;
">
<span onclick="shareWeixin(); wzhitnew(64);
">
<a title="分享到微信" href="javascript: void(0);
">
<img src="http: //pubimage.360doc.com/wz/weixin.gif">
</a>
</span>
</div>
<div style="float: left;" id="bdshare" class="bdshare_t bds_tools get-codes-bdshare share-bar-btn2">
<span onclick="wzhitnew(28);">
<a class="bds_qzone" title="分享到QQ空间" href="javascript:void(0);">
</a>
</span>
<span onclick="wzhitnew(31);">
<a class="bds_tsina" title="分享到新浪微博" href="javascript:void(0);">
</a>
</span>
<span onclick="wzhitnew(32);">
<a class="bds_tqq" title="分享到腾讯微博" href="javascript:void(0);">
</a>
</span>
</div>
</div>
</td>
<td width="24">
<a>
<img style="cursor: pointer;" onclick="setTimeout(showShareLayer2, 10);wzhitnew(34);"
src="http://pubimage.360doc.com/wz/tb10.gif">
</a>
<div style="margin-top: 5px; margin-left: -100px; display: none; z-index: 1000;"
id="fenxiangLayer1" class="fenxiang">
<div id="bdshare" class="bdshare_t bds_tools get-codes-bdshare share-bar-btn">
<table style="background-color: rgb(255, 255, 255);" cellSpacing="0" cellPadding="0"
width="108">
<tbody>
<tr>
<td width="12">
</td>
<td onclick="wzhitnew(29);" height="29">
<a class="bds_renren" title="分享到人人网" href="javascript:void(0);">
人人网
</a>
</td>
</tr>
<tr>
<td width="12">
</td>
<td onclick="wzhitnew(30);" height="29">
<a class="bds_kaixin001" title="分享到开心网" href="javascript:void(0);">
开心网
</a>
</td>
</tr>
<tr>
<td width="12">
</td>
<td onclick="wzhitnew(33);" height="29">
<a class="bds_tsohu" title="分享到搜狐微博" href="javascript:void(0);">
搜狐微博
</a>
</td>
</tr>
<tr>
<td width="12">
</td>
<td height="29">
<a onclick="showdivemail1();wzhitnew(35);" href="javascript:void(0);">
<div class="bds_py">
推荐给朋友
</div>
</a>
</td>
</tr>
<tr>
<td width="12">
</td>
<td height="29">
<a href="javascript:void(0);">
<div class="bds_jubao" onclick="showAlertLayer1();wzhitnew(36);">
举报
</div>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<div style="display: none; position: absolute;" id="AlertArt2">
</div>
<div style="display: none; position: absolute; z-index: 1000;" id="SendToFriends2">
</div>
</div>
< /td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td height="10">
</td>
</tr>
</tbody>
</table>
相关推荐
Conect-to-SAP.rar Conect-to-SAP.rar Conect-to-SAP.rar Conect-to-SAP.rar
JuniperSetupClientInstaller和Juniper networks conect 7.1
vba Conect to SAP 给大家参考,希望对大家能有点帮助
How to use SDM to conect cisco device using gns3 中文
Conect4
conect-decorator 将一个javascript function 通过 conectDecorator 方法,让这个function与其它的方法相链接,使其它方法可以操作被链接的对象,达到装饰者模式的效果。 npm i conect-decorator --save 下面是例子...
该资源是mysql-connector-java-8.0.11.jar包和c3p0连接池的三个jar包,一共四个jar包,用来驱动MySQL数据库和Java的连接
nodejs-mongodb-conect- sbse phle hm comd me ye path likhte h C:\ MongoDB的\斌 => usi kmand我 蒙哥 => dusre coment open krte h 蒙哥 =>创建数据 =>使用数据BACE KA名称 使用_____traniosite =>数据同步...
CONECT是用于设计,设置,调试和测试家庭和楼宇自动化系统的软件。 它将支持各种总线系统,例如EIB。
语言:español (Latinoamérica) 您所有的社交媒体都集中在一个地方 您所有的社交媒体都集中在一个地方
所有的社交媒体在一个地方 所有的社交媒体在一个地方 支持语言:español (Latinoamérica)
db2connect db2connect
u盘修复工具可以修复类似u盘无法格式化,U盘量产,U盘无法格式化等错误,简单实用,当您的U盘或者mp3遇到以上情况的话,使用本工具能让您快速的解决问题!
VBA conect LDAP in MS2003 ACCESS
第三个项目(在此平台上)...现在,您将在这里找到具有网络拓扑结构的实验室(不同于思科系统课程中包含的实验室),这些实验室的重点是那些不符合CCNA 1-4 +安全性+ IdT +语音的中级用户和网络研讨会。...
SIM300Z TO CONECT SMS AND MOBIPHONE
介绍h245协议,请各位放心下载
Atemega 32 conect with ds1307 to get time and display on LCD
Sample program to conect opc cliente-server in windows.
This program can show how to conect to a mysql database and manipulate data