oracle中left join和right join和inner join的區別

來源:互聯網
上載者:User

 

oracle中left join和right join的區別  

通俗的講:  

   A   left   join    B    的串連的記錄數與A表的記錄數同  

   A   right   join    B    的串連的記錄數與B表的記錄數同    

   A   left   join    B    等價B   right   join    A 

 

table    A:  

   Field_K,    Field_A  

  1                        a  

  3                        b  

  4                        c  

   

  table    B:  

   Field_K,    Field_B  

  1                        x  

  2                        y  

  4                        z  

   

  select    a.Field_K,    a.Field_A,    b.Field_K,    b.Field_B  

  from    a   left   join    b   on    a.Field_K=b.Field_K  

   

   Field_K          Field_A          Field_K          Field_B          

  ----------    ----------    ----------    ----------    

  1                      a                     1                      x                    

  3                      b                     NULL               NULL  

  4                      c                     4                      z                    

   

  select    a.Field_K,    a.Field_A,    b.Field_K,    b.Field_B  

  from    a   right   join    b   on    a.Field_K=b.Field_K  

   

   Field_K          Field_A          Field_K          Field_B          

  ----------    ----------    ----------    ----------    

  1                      a                     1                      x                    

  NULL               NULL               2                      y                    

  4                      c                     4                      z      --

舉個例子:  

   假設a表和b表的資料是這樣的。  

   a                          b    

   id      name  id      stock   

  1  a             1         15  

  2          b             2         50  

  3          c                   

   

  select   *   from    a   inner   join    b   on    a.id=b.id  

   這個文法是串連查詢中的內串連,它產生的結果是  

   兩個表相匹配的記錄出現在結果清單中。  

   根據上面的表,出現的結果是這樣的  

   a.id      name      b.id      stock  

  1        a             1         15  

  2              b             2         50  

  ----------------------------  

  select   *   from    a,b   where    a.id=b.id  

   這個文法是內串連的另外一種寫法,其執行結果與inner   join    一樣  

   

  --------------------------------    

   

  select   *   from    a   left/right   join    b   on    a.id=b.id  

   這個是外串連文法中的左外串連或右外串連  

   如果是左外串連的話,它將顯示a表的所有記錄,  

  select    a.*,b.*   from    a   left   join    b   on    a.id=b.id  

   查詢的結果是這樣的:  

   a.id      name      b.id      stock  

  1          a         1             15  

  2                b         2             50  

  3                c       null         null   

  --------------------------------------------  

   如果是右外串連的話,它將顯示b表的所有記錄,  

  select    a.*,b.*   from    a   right   join    b   on    a.id=b.id  

   查詢的結果是這樣的:  

   a.id      name      b.id      stock  

  1          a         1             15  

  2                b         2             50   

--

select    a.*,b.*   from    a   left   join    b   on    a.k   =    b.k    

  select    a.*,b.*   from    a   left   outer   join    b   on    a.k   =b.k  

  ----------上面兩種一樣left    join是left    outer    join的簡寫  

  select    a.*,b.*   from    a   left   inner   join    b   on    a.k   =    b.k    

   沒有這種寫法,錯誤的語句.

轉自http://blog.163.com/anly_liu/blog/static/428323142009773856582/

聯繫我們

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