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 :右外串連,結果包含滿足條件的行及右側表中的全部行。
 

相關文章

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.