oracle connect by用法篇

來源:互聯網
上載者:User

標籤:tab   基本文法   oracle   結構   evel   max   時間   遞增   start   

1、基本文法

select * from table [start with condition1]    connect by [prior] id=parentid
  • 1
  • 2
  • 1
  • 2

一般用來尋找存在父子關係的資料,也就是樹形結構的資料;其返還的資料也能夠明確的區分出每一層的資料。

  • start with condition1 是用來限制第一層的資料,或者叫根節點資料;以這部分資料為基礎來尋找第二層資料,然後以第二層資料尋找第三層資料以此類推。

  • connect by [prior] id=parentid 這部分是用來指明Oracle在尋找資料時以怎樣的一種關係去尋找;比如說尋找第二層的資料時用第一層資料的id去跟表裡面記錄的parentid欄位進行匹配,如果這個條件成立那麼尋找出來的資料就是第二層資料,同理尋找第三層第四層…等等都是按這樣去匹配。

prior還有一種用法:

select * from table [start with condition1]    connect by id= [prior] parentid
  • 1
  • 2
  • 1
  • 2
  • 這種用法就表示從下往上尋找資料,可以理解為從葉子節點往上尋找父級幾點,用第一層資料的parentid去跟表記錄裡面的id進行匹配,匹配成功那麼尋找出來的就是第二層資料;上面的那種就是從父級節點往下尋找葉子節點。

其他特性

  1. level關鍵字,代表樹形結構中的層級編號;第一層是數字1,第二層數字2,依次遞增。
  2. CONNECT_BY_ROOT方法,能夠擷取第一層集結點結果集中的任意欄位的值;例CONNECT_BY_ROOT(欄位名)。

2、下面來貼兩個例子

2.1 從根節點尋找葉子節點

select t.*, level, CONNECT_BY_ROOT(id)  from tab_test t start with t.id = 0connect by prior t.id = t.fid;
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2.2 從葉子節點尋找上層節點

--第一種,修改prior關鍵字位置select t.*, level, CONNECT_BY_ROOT(id)  from tab_test t start with t.id = 4connect by t.id = prior t.fid;--第二種,prior關鍵字不動 調換後面的id=fid邏輯關係的順序select t.*, level, CONNECT_BY_ROOT(id)  from tab_test t start with t.id = 4connect by prior t.fid = t.id;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、寫幾個平常用到的其他一些用法

3.1 產生數字序列結果集

  • 使用rownum實現1到10的序列。
select rownum from dual connect by rownum<=10;
  • 1
  • 1

結果集如下:

  • 使用level實現1到10的序列。
select level from dual connect by level<=10;
  • 1
  • 1

結果集如下:

3.2 查詢目前時間往前的12周的開始時間、結束時間、第多少周

select sysdate - (to_number(to_char(sysdate - 1, ‘d‘)) - 1) -       (rownum - 1) * 7 as startDate,       sysdate + (7 - to_number(to_char(sysdate - 1, ‘d‘))) -       (rownum - 1) * 7 as endDate,       to_number(to_char(sysdate, ‘iw‘)) - rownum + 1 as weekIndex  from dualconnect by level<= 12;--將level改成rownum可以實現同樣的效果
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

  • d 表示一星期中的第幾天
  • iw 表示一年中的第幾周

3.3 字串分割,由一行變為多行

  1. 比如說分割01#02#03#04這種有規律的字串
select REGEXP_SUBSTR(‘01#02#03#04‘, ‘[^#]+‘, 1, rownum) as newport     from dual connect by rownum <= REGEXP_COUNT(‘01#02#03#04‘, ‘[^#]+‘);
  • 1
  • 2
  • 1
  • 2

4、省略prior關鍵字時資料的返回策略

構造一個結果集,其中包含兩條資料;然後查詢level為1,2,3層的資料。

select t.*, level  from (select 1 as num from dual        union        select 2 as num from dual        ) tconnect by level <= 3;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

  1. 從上面的結果可以看出來省略prior關鍵字時第1層的資料就是初始結果集,第2層的資料是初始結果集的兩倍,第3層的資料是初始結果集的3倍;假設初始結果集的記錄為n條,查詢m層的記錄,則返回的記錄數就是: 條記錄。

  2. 在省略prior關鍵字對資料進行操作時需要特別注意,返回的資料不一定是你所期望的那樣。

5、下面再看看幾個例子,針對多條結果集當省略prior關鍵字時怎樣獲得正確的返回結果

5.1 有下面一個結果集

想要實現1-5,20-30的資料遞增返回1、2、3、4、5、20、21、22、23、24、25、26、27、28、29、30總共16條記錄。

SQL如下:

with temp0 as (   select t.range_num,          REGEXP_SUBSTR(t.range_num, ‘[^-]+‘, 1, 1) minNum, --最小num          REGEXP_SUBSTR(t.range_num, ‘[^-]+‘, 1, 2) maxNum --最大num     from range_table t)select t1.range_num ,t2.lv        from temp0 t1 join (         select level lv from dual                CONNECT BY LEVEL <= (select max(maxNum) from temp0 )          ) t2          on (t2.lv >=t1.minNum and t2.lv <=t1.maxNum);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13


上面的sql中是先求出range_num的最大值與最小值,然後利用connect by 特性產生數值序列結果集,最後讓兩個結果集關聯得到需要的結果。

5.2 再看稍微複雜的結果集,輸出結果格式跟上面一樣

SQL如下:

with temp0 as (  select b.range_num,         REGEXP_SUBSTR(b.range_num, ‘[^,]+‘, 1, c.lv) as newport,         REGEXP_SUBSTR(REGEXP_SUBSTR(b.range_num, ‘[^,]+‘, 1, c.lv), ‘[^-]+‘, 1, 1) as minNum,         REGEXP_SUBSTR(REGEXP_SUBSTR(b.range_num, ‘[^,]+‘, 1, c.lv), ‘[^-]+‘, 1, 2) as maxNum  from (select regexp_count(a.range_num, ‘[^,]+‘) AS cnt,                range_num          from range_table a) b  join (select LEVEL lv from dual CONNECT BY LEVEL <= 50) c   --這裡的50表示的是range_num通過,分割後的數量,這裡寫死了50也可以sql動態max出來        on c.lv <= b.cnt)select t1.range_num,t2.lv     from temp0 t1     join (       select level lv from dual          CONNECT BY LEVEL <= (             select max(to_number(maxNum)) from temp0          )       ) t2     on ((t2.lv >=t1.minNum and t2.lv <=t1.maxNum));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

oracle connect by用法篇 (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.