sql連線(內連線 外連線 全連線)

2021-05-11 15:18:47 字數 2194 閱讀 3068

現有table_for_report_1和table_for_report_2,詳情如下:

table_for_report_1有num欄位,c1,c2,c3欄位。

資料如下:

num                c1  c2  c3

1 15001346690 11 12 13

2 13329921100 21 22 23

3 18900000000 31 32 33

4 13811111111 41 42 43

table_for_report_2有num欄位,c4,c5欄位。

資料如下:

num                c4  c5 

1 13822222222 34 35

2 15001346690 14 15

3 13811111111 24 25

內連線:

select t1.num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1, table_for_report_2 t2

where t1.num=t2.num

結果:num                c1  c2 c3 c4 c5

1 15001346690 11 12 13 14 15

2 13811111111 41 42 43 24 25

外連線(左外連線和右外連線):

1)左外連線:

select t1.num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1, table_for_report_2 t2

where t1.num=t2.num(+)

或者select t1.num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1 left outer join table_for_report_2 t2

on (t1.num=t2.num)

結果:num                c1  c2 c3 c4 c5

1 15001346690 11 12 13 14 15

2 13811111111 41 42 43 24 25

3 18900000000 31 32 33  

4 13329921100 21 22 23  

2)右外連線:

select t2.num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1, table_for_report_2 t2

where t1.num(+)=t2.num

或者select t2.num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1 right outer join table_for_report_2 t2

on (t1.num=t2.num)

結果:num                c1  c2 c3 c4 c5

1 15001346690 11 12 13 14 15

2 13811111111 41 42 43 24 25

3 13822222222                34 35

全連線:

select (case when (t1.num is null) then t1.num||t2.num else t1.num end) num,t1.c1,t1.c2,t1.c3,t2.c4,t2.c5

from table_for_report_1 t1 full outer join table_for_report_2 t2

on (t1.num=t2.num)

結果:num                c1  c2 c3 c4 c5

1 15001346690 11 12 13 14 15

2 13811111111 41 42 43 24 25

3 18900000000 31 32 33  

4 13329921100 21 22 23  

5 13822222222                34 35

SQL 左外連線,右外連線,全連線,內連線

例子1 a表 id name b表 id job parent id 1 張3 1 23 1 2 李四 2 34 2 3 王武 3 34 4 a.id同parent id 存在 關係內連線 select a.b.from a inner join b on a.id b.parent id 結果是 ...

SQL 左外連線,右外連線,全連線,內連線,自連線

左連線 返回包括左表中的所有記錄和右表中聯結字段相等的記錄 select a.rid,a.picname,b.trpid,b.rid,b.picname,b.picurl from tc restaurants a left join tc restaurants pictures b on a.r...

SQL之全外連線,左外連線,右外連線,內連線

1.內連線 內聯接使用比較運算子 使用像 或 之類的比較運算子 根據每個表共有的列的值匹配兩個表中的行,根據這兩張表中相同列的條件,得出其交集。沒有inner join,形成的中間表為兩個表的笛卡爾積 select from customers c,orders o where c.id o.cus...