Oracle中左右外串連詳解

來源:互聯網
上載者:User

標籤:

資料表的串連有: 

1、內串連(自然串連): 只有兩個表相匹配的行才能在結果集中出現 
2、外串連: 包括 
(1)左外串連(左邊的表不加限制) 
(2)右外串連(右邊的表不加限制) 
(3)全外串連(左右兩表都不加限制) 
3、自串連(串連發生在一張基表內) 

以下是三種串連的區分:
select a.studentno, a.studentname, b.classname
      from students a, classes b
      where a.classid(+) = b.classid;
(另外一種寫法:
select a.studentno,a.studentname,b.classname
        from students right join class on students.classid=class.classid

STUDENTNO STUDENTNAM CLASSNAME
---------- ---------- ------------------------------
            1 周虎          一年級一班
            2 周林          一年級二班
                             一年級三班
以上語句是右串連:
即"(+)"所在位置的另一側為串連的方向,右串連說明等號右側的所有
記錄均會被顯示,無論其在左側是否得到匹配。也就是說上例中,無
論會不會出現某個班級沒有一個學生的情況,這個班級的名字都會在
查詢結構中出現。
即是右串連是以右邊這個表為基準,左表不足的地方用NULL填充


反之: 
select a.studentno, a.studentname, b.classname
       from students a, classes b
      where a.classid = b.classid(+);

(另外一種寫法:
select a.studentno,a.studentname,b.classname
        from students left join class on students.classid=class.classid


STUDENTNO STUDENTNAM CLASSNAME
---------- ---------- ------------------------------
            1 周虎          一年級一班
            2 周林          一年級二班
            3 鐘林達

以上則是左串連,無論這個學生有沒有一個能在一個班級中得到匹配的部門號,
這個學生的記錄都會被顯示。

即是左串連是以左邊這個表為基準,右表表不足的地方用NULL填充

select a.studentno, a.studentname, b.classname
       from students a, classes b
      where a.classid = b.classid;
(另外一種寫法:
select a.studentno,a.studentname,b.classname
        from students inner join class on students.classid=class.classid

這個則是通常用到的內串連,顯示兩表都合格記錄

總之:

左串連顯示左邊全部的和右邊與左邊相同的 
右串連顯示右邊全部的和左邊與右邊相同的 
內串連是只顯示滿足條件的!

Oracle中左右外串連詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.