SQL中left join和inner join配合使用,innerjoin

來源:互聯網
上載者:User

SQL中left join和inner join配合使用,innerjoin

left join on 是左外聯結,資料以左邊的表為基準,右邊的表資料哪怕為null也會查詢出來,

而inner join on則是左右兩邊的表的資料必須相互對應,如果有null則會去掉該條資料,如果

右邊或者左邊表的資料都為null,則查詢出來是沒有結果集的。兩個聯結配合起來使用效果極好。

因為有些需求需要我們配合這兩個聯結查詢。


SQL中 inner join、 left join 、right join、 outer join之間的不同

舉個例子你就能知道了!
A表(a1,b1,c1) B表(a2,b2)
a1 b1 c1 a2 b2
01 數學 95 01 張三
02 語文 90 02 李四
03 英語 80 04 王五
select A.*,B.* from A
inner join B on(A.a1=B.a2)
結果是:
a1 b1 c1 a2 b2
01 數學 95 01 張三
02 語文 90 02 李四

select A.*,B.* from A
left outer join B on(A.a1=B.a2)
結果是:
a1 b1 c1 a2 b2
01 數學 95 01 張三
02 語文 90 02 李四
03 英語 80 NULL NULL

select A.*,B.* from A
right outer join B on(A.a1=B.a2)
結果是:
a1 b1 c1 a2 b2
01 數學 95 01 張三
02 語文 90 02 李四
NULL NULL NULL 04 王五

select A.*,B.* from A
full outer join B on(A.a1=B.a2)
結果是:
a1 b1 c1 a2 b2
01 數學 95 01 張三
02 語文 90 02 李四
03 英語 80 NULL NULL
NULL NULL NULL 04 王五
 
SQL server 2005 串連查詢中(inner join ,left join,right join 的不同)

inner join: 內串連,結果只包含滿足條件的列。
left join:左外串連,結果包含滿足條件的行及左側表中的全部行。
right join :右外串連,結果包含滿足條件的行及右側表中的全部行。
 

聯繫我們

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