一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Connect By、Level、Start With的使用

 風(fēng)中的眼睛_ 2013-04-22

ORACLE--Connect By、Level、Start With的使用(Hierarchical query-層次查詢)

Syntax 1CONNECT BY [NOCYCLE] <condition> START WITH <condition>
Syntax 2START WITH <condition> CONNECT BY [NOCYCLE] <condition>

參考網(wǎng)址:http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:489772591421

            http:///reference/connectby.html

            http://www./connect_by.jsp

           http://philip./sql/trees.html
查找員工編號(hào)為7369的領(lǐng)導(dǎo):

1 SELECT LEVEL,E.* FROM EMP E CONNECT BY PRIOR E.MGR = E.EMPNO  START WITH E.EMPNO = 7876
2 ORDER BY LEVEL DESC

"start with" -- this identifies all LEVEL=1 nodes in the tree

"connect by" -- describes how to walk from the parent nodes above to their children and 
their childrens children.

Easiest to use an example on emp. If we start with "where mgr is NULL", we generate the 
set of employees that have no mgr (they are the top of the tree). If we

CONNECT BY PRIOR EMPNO = /* current */ MGR

that will take all of the PRIOR records (the start with at first) and find all records 
such that the MGR column equals their EMPNO (find all the records of people managed by 
the people we started with).


使用WITH語(yǔ)句優(yōu)化查詢結(jié)果:優(yōu)化等級(jí)

復(fù)制代碼
 1 WITH A AS
2 (SELECT MAX(LEVEL) + 1 LVL
3 FROM EMP E
4 CONNECT BY PRIOR E.MGR = E.EMPNO
5 START WITH E.EMPNO = 7876
6 ORDER BY LEVEL DESC)
7 SELECT A.LVL 最高等級(jí)加1,
8 LEVEL 當(dāng)前等級(jí),
9 A.LVL - LEVEL 優(yōu)化后等級(jí),
10 E.*  FROM A,
11 EMP E CONNECT BY PRIOR E.MGR = E.EMPNO START WITH E.EMPNO = 7876 ORDER BY LEVEL DESC
復(fù)制代碼

查找員工編號(hào)為7839的所有下屬(7839為king):

1 SELECT LEVEL 等級(jí), E.*
2 FROM EMP E
3 CONNECT BY PRIOR E.EMPNO = E.MGR
4 START WITH E.EMPNO = 7839

--構(gòu)造整個(gè)的層次結(jié)構(gòu)

1 select lpad(' ',level*2,' ')||ename ename, empno, mgr
2 from emp
3 START WITH MGR IS NULL
4 CONNECT BY PRIOR EMPNO = MGR

So, KING is the start with set then JONES BLAKE and CLARK fall under him. Each of them 
becomes the PRIOR record in turn and their trees are expanded.



使用Connect By 結(jié)合 level構(gòu)造虛擬行:

1 SELECT LEVEL FROM DUAL CONNECT BY LEVEL < 5

使用rownum實(shí)現(xiàn)類似的功能:

1 SELECT LEVEL FROM DUAL CONNECT BY LEVEL < 5

---------------------待續(xù)-----------------------

使用UNION ALL構(gòu)造兩層節(jié)點(diǎn)的樹(shù):

視圖如下所示:

復(fù)制代碼
 1 CREATE OR REPLACE VIEW TREE_VIEW AS
2 SELECT
3 '1' AS rootnodeid,
4 'xxxx有限責(zé)任公司' AS treename,
5 '-1' AS parent_id
6 FROM dual
7 UNION
8 SELECT
9 to_char(d.deptno),
10 d.dname || '_' ||d.loc,
11 '1' AS parent_id
12 FROM dept d;
復(fù)制代碼

查詢語(yǔ)句:

1 SELECT T.*, LEVEL
2 FROM TREE_VIEW T
3 START WITH T.PARENT_ID = '-1'
4 CONNECT BY PRIOR T.ROOTNODEID = T.PARENT_ID

-----以下為更新內(nèi)容:

1、先查看總共有幾個(gè)等級(jí):

1 SELECT COUNT(LEVEL)
2   FROM EMP E
3 CONNECT BY PRIOR E.EMPNO = E.MGR
4  START WITH E.MGR IS NULL;

2、查看每個(gè)等級(jí)的人數(shù)。主要是通過(guò)LEVEL進(jìn)行GROUP BY

1 SELECT COUNT(LEVEL)
2   FROM EMP E
3 CONNECT BY PRIOR E.EMPNO = E.MGR
4  START WITH E.MGR IS NULL
5  GROUP BY LEVEL;

3、Oracle 10g提供了一個(gè)簡(jiǎn)單的connect_by_isleaf=1,

0 表示非葉子節(jié)點(diǎn)

1 SELECT LEVEL AS 等級(jí), CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn), E.*
2   FROM EMP E
3 CONNECT BY PRIOR E.EMPNO = E.MGR
4  START WITH E.MGR IS NULL

4、SYS_CONNECT_BY_PATH

Oracle 9i提供了sys_connect_by_path(column,char),其中column 是字符型或能自動(dòng)轉(zhuǎn)

換成字符型的列名。它的主要目的就是將父節(jié)點(diǎn)到當(dāng)前節(jié)點(diǎn)的”path”按照指定的模式展現(xiàn)出現(xiàn)。這個(gè)函數(shù)只能使用在層次查詢中。

1 SELECT LEVEL AS 等級(jí),
2        CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn),
3        LPAD(' ', LEVEL * 2 - 1) || SYS_CONNECT_BY_PATH(ENAME, '=>')
4   FROM EMP E
5 CONNECT BY PRIOR E.EMPNO = E.MGR
6  START WITH E.MGR IS NULL;

5、修剪樹(shù)枝和節(jié)點(diǎn):

    過(guò)濾掉編號(hào)是7566的數(shù)據(jù)(修剪節(jié)點(diǎn)),他指的是把這個(gè)節(jié)點(diǎn)給裁掉,但是并沒(méi)有破壞樹(shù)結(jié)構(gòu),它的子節(jié)點(diǎn)還是可以正常的顯示。

復(fù)制代碼
1 SELECT LEVEL AS 等級(jí),
2        CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn),
3        LPAD(' ', LEVEL * 2 - 1) || SYS_CONNECT_BY_PATH(ENAME, '=>'),
4        E.*
5   FROM EMP E
6 WHERE e.empno != 7566
7 CONNECT BY PRIOR E.EMPNO = E.MGR
8  START WITH E.MGR IS NULL;
復(fù)制代碼

裁掉編號(hào)是7698的節(jié)點(diǎn)和它的子節(jié)點(diǎn):

復(fù)制代碼
1 SELECT LEVEL AS 等級(jí),
2        CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn),
3        LPAD(' ', LEVEL * 2 - 1) || SYS_CONNECT_BY_PATH(ENAME, '=>'),
4        E.*
5   FROM EMP E
6 CONNECT BY PRIOR E.EMPNO = E.MGR
7        AND E.EMPNO != 7698
8  START WITH E.MGR IS NULL;
復(fù)制代碼

6、CONNECT_BY_ROOT的使用,oracle10g新增connect_by_root,用在列名之前表示此行的根節(jié)點(diǎn)的相同列名的值。

復(fù)制代碼
1 SELECT LEVEL AS 等級(jí),
2        CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn),
3        CONNECT_BY_ROOT ENAME,
4        LPAD(' ', LEVEL * 2 - 1) || SYS_CONNECT_BY_PATH(ENAME, '=>'),
5        E.*
6   FROM EMP E
7 CONNECT BY PRIOR E.EMPNO = E.MGR
8  START WITH E.MGR IS NULL;
復(fù)制代碼

 

對(duì)于層次查詢?nèi)绻胦rder by排序,比如order by last_name則是先做完層次獲得level,然后按last_name 排序,這樣破壞了層次,比如特別關(guān)注某行的深度,按level 排序,也是會(huì)破壞層次的。在oracle10g中,增加了siblings 關(guān)鍵字的排序。

語(yǔ)法:order siblings by <expre>

它會(huì)保護(hù)層次,并且在每個(gè)等級(jí)中按expre排序。

復(fù)制代碼
1 SELECT LEVEL AS 等級(jí),
2        CONNECT_BY_ISLEAF AS 是否是葉子節(jié)點(diǎn),
3        LPAD(' ', LEVEL * 2 - 1) || SYS_CONNECT_BY_PATH(ENAME, '=>'),
4        E.*
5   FROM EMP E
6 CONNECT BY PRIOR E.EMPNO = E.MGR 
7  START WITH E.MGR IS NULL
8  ORDER SIBLINGS BY  E.ENAME;
復(fù)制代碼

connect_by_iscycle(存在循環(huán),將返回1,否則返回0)

The CONNECT_BY_ISCYCLE pseudocolumn returns 1 if the current row has a child which is also its ancestor. Otherwise it returns 0. 
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.

I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我們成為怎樣的人是我們自己的選擇。沒(méi)有人會(huì)來(lái)拯救你,你必須要自己拯救自己。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多

    欧美日韩国产成人高潮| 亚洲精品中文字幕一二三| 有坂深雪中文字幕亚洲中文| 亚洲一区二区三区在线免费| 隔壁的日本人妻中文字幕版| 女人精品内射国产99| 国产精品成人一区二区在线 | 欧美在线观看视频免费不卡| 亚洲超碰成人天堂涩涩| 久久精品国产在热久久| 午夜精品一区二区三区国产| 久久精品久久久精品久久| 欧美日韩国产福利在线观看| 亚洲深夜精品福利一区| 深夜福利亚洲高清性感| 五月综合激情婷婷丁香| 欧美熟妇喷浆一区二区| 久久精品免费视看国产成人| 亚洲做性视频在线播放| 日韩欧美高清国内精品| 日本高清二区视频久二区| 激情偷拍一区二区三区视频| 国产一级一片内射视频在线| 东京热男人的天堂一二三区| 亚洲成人精品免费在线观看| 美女被草的视频在线观看| 亚洲最新的黄色录像在线| 日韩成人中文字幕在线一区| 99视频精品免费视频播放| 91人妻人人做人碰人人九色| 精品偷拍一区二区三区| 亚洲中文字幕亲近伦片| 国产中文字幕久久黄色片| 午夜传媒视频免费在线观看| 富婆又大又白又丰满又紧又硬| 日韩欧美二区中文字幕| 在线观看国产成人av天堂野外| 亚洲国产成人精品一区刚刚| 亚洲国产av在线视频| 亚洲a级一区二区不卡| 精品香蕉国产一区二区三区|