`
小杨学JAVA
  • 浏览: 885294 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ORACLE start with conect by 详解

 
阅读更多

<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>

                                                            &nbsp;[NOCYCLE] &lt;condition&gt;&nbsp;

                                                            <a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/START_WITH.htm">

                                                                START WITH

                                                            </a>

                                                            &nbsp;&lt;condition&gt;

                                                        </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>

                                                            &nbsp;&lt;condition&gt;&nbsp;

                                                            <a style="color: rgb(26, 139, 200); text-decoration: none;" href="http://psoug.org/definition/CONNECT_BY.htm">

                                                                CONNECT BY

                                                            </a>

                                                            &nbsp;[NOCYCLE] &lt;condition&gt;

                                                        </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;">

                                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

                                            <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;">

                                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

                                            <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;">

                                            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

                                            <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&nbsp;

                                            <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&nbsp;

                                            <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&nbsp;

                                            <br>

                                            such that the MGR column equals their EMPNO (find all the records of people

                                            managed by&nbsp;

                                            <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&nbsp;

                                            <br>

                                            becomes the&nbsp;

                                            <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;">

                                                    &lt;

                                                </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;">

                                                    &lt;

                                                </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;">

                                                                                                            =&gt;

                                                                                                        </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">

                                                            &nbsp; &nbsp; 过滤掉编号是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;">

                                                                                                    =&gt;

                                                                                                </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;">

                                                                                    = &gt;

                                                                                    < /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;">

                                                                                                            =&gt;

                                                                                                        </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;">

                                                            &nbsp;

                                                        </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&nbsp;排序,也是会破坏层次的。在oracle10g中,增加了siblings

                                                            关键字的排序。

                                                        </p>

                                                        <p style="margin: 10px auto;" align="left">

                                                            语法:order siblings by &lt;expre&gt;

                                                        </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;">

                                                                                                    =&gt;

                                                                                                </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.&nbsp;

                                                            <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>

                                    &nbsp; & gt; & nbsp;

                                    < span id="cname" onclick="wzhitnew(60)">

                                        <a href="http://www.360doc.com/userhome.aspx?userid=11947209&amp;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&amp;dai=1&amp;cfv=12&amp;cpa=1&amp;col=zh-cn&amp;dis=0&amp;xuanting=0&amp;n=360doc168_cpr&amp;conOP=0&amp;scale=&amp;skin=tabcloud_skin_1&amp;rsi0=650&amp;rsi1=120&amp;rsi5=4&amp;ltr=&amp;ltu=http%3A%2F%2Fwww.360doc.com%2Fcontent%2F13%2F0422%2F16%2F11947209_280153192.shtml&amp;pcs=1423x761&amp;rss0=&amp;rss1=&amp;rss2=&amp;rss3=&amp;rss4=&amp;rss5=&amp;rss6=&amp;rss7=&amp;rad=&amp;pis=10000x10000&amp;aurl=&amp;psr=1440x900&amp;pss=1423x7845&amp;stid=5&amp;tpr=1395824780709&amp;lunum=6&amp;ch=0&amp;at=103&amp;qn=b09f911f039c88e8&amp;ps=7845x239&amp;tn=baiduCustSTagLinkUnit&amp;ts=1&amp;c01=0&amp;td_id=1350774&amp;adn=0&amp;cad=1&amp;ccd=24&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;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&amp;dcb=BAIDU_DUP2_define&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dbv=0&amp;dci=0&amp;dri=0&amp;dis=0&amp;dai=1&amp;dds=&amp;drs=3&amp;dvi=1395372013&amp;ltu=http%3A%2F%2Fwww.360doc.com%2Fcontent%2F13%2F0422%2F16%2F11947209_280153192.shtml&amp;liu=&amp;ltr=&amp;lcr=&amp;ps=7845x239&amp;psr=1440x900&amp;par=1440x860&amp;pcs=1423x761&amp;pss=1423x7845&amp;pis=-1x-1&amp;cfv=12&amp;ccd=24&amp;chi=1&amp;cja=true&amp;cpl=0&amp;cmi=0&amp;cce=true&amp;col=zh-cn&amp;cec=utf-8&amp;cdo=-1&amp;tsr=560&amp;tlm=1395824780&amp;tcn=1395824781&amp;tpr=1395824780709&amp;dpt=none&amp;coa=c01%3D0&amp;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);">

                                                                                    &nbsp;&nbsp;&nbsp;&nbsp;人人网

                                                                                </a>

                                                                            </td>

                                                                        </tr>

                                                                        <tr>

                                                                            <td width="12">

                                                                            </td>

                                                                            <td onclick="wzhitnew(30);" height="29">

                                                                                <a class="bds_kaixin001" title="分享到开心网" href="javascript:void(0);">

                                                                                    &nbsp;&nbsp;&nbsp;&nbsp;开心网

                                                                                </a>

                                                                            </td>

                                                                        </tr>

                                                                        <tr>

                                                                            <td width="12">

                                                                            </td>

                                                                            <td onclick="wzhitnew(33);" height="29">

                                                                                <a class="bds_tsohu" title="分享到搜狐微博" href="javascript:void(0);">

                                                                                    &nbsp;&nbsp;&nbsp;&nbsp;搜狐微博

                                                                                </a>

                                                                            </td>

                                                                        </tr>

                                                                        <tr>

                                                                            <td width="12">

                                                                            </td>

                                                                            <td height="29">

                                                                                <a onclick="showdivemail1();wzhitnew(35);" href="javascript:void(0);">

                                                                                    <div class="bds_py">

                                                                                        &nbsp;&nbsp;&nbsp;&nbsp;推荐给朋友

                                                                                    </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);">

                                                                                        &nbsp;&nbsp;&nbsp;&nbsp;举报

                                                                                    </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>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics