內連線 外連線 全連線 where的區別

2021-09-02 08:49:10 字數 3327 閱讀 1777

1、where條件

with student as(

select '1' id,'張三' name,'1' tid from dual union all

select '2' id,'李四' name,'2' tid from dual union all

select '3' id,'王五' name,'1' tid from dual union all

select '4' id,'趙六' name,'5' tid from dual

),teacher as(

select '1' id,'***' name from dual union all

select '2' id,'張老師' name from dual union all

select '3' id,'鄭老師' name from dual

)select * from student s,teacher t where s.tid=t.id

id name tid id name

1 張三 1 1 ***

2 李四 2 2 張老師

3 王五 1 1 ***

2、內連線

with student as(

select '1' id,'張三' name,'1' tid from dual union all

select '2' id,'李四' name,'2' tid from dual union all

select '3' id,'王五' name,'1' tid from dual union all

select '4' id,'趙六' name,'5' tid from dual

),teacher as(

select '1' id,'***' name from dual union all

select '2' id,'張老師' name from dual union all

select '3' id,'鄭老師' name from dual

)select * from student s join teacher t on s.tid=t.id

id name tid id name

1 張三 1 1 ***

2 李四 2 2 張老師

3 王五 1 1 ***

3、外連線

3.1 左外連線

with student as(

select '1' id,'張三' name,'1' tid from dual union all

select '2' id,'李四' name,'2' tid from dual union all

select '3' id,'王五' name,'1' tid from dual union all

select '4' id,'趙六' name,'5' tid from dual

),teacher as(

select '1' id,'***' name from dual union all

select '2' id,'張老師' name from dual union all

select '3' id,'鄭老師' name from dual

)select * from student s left join teacher t on s.tid=t.id

或者select * from student s left join teacher t on s.tid=t.id(+)

id name tid id name

3 王五 1 1 ***

1 張三 1 1 ***

2 李四 2 2 張老師

4 趙六 5

3.2 右外連線

with student as(

select '1' id,'張三' name,'1' tid from dual union all

select '2' id,'李四' name,'2' tid from dual union all

select '3' id,'王五' name,'1' tid from dual union all

select '4' id,'趙六' name,'5' tid from dual

),teacher as(

select '1' id,'***' name from dual union all

select '2' id,'張老師' name from dual union all

select '3' id,'鄭老師' name from dual

)select * from student s right join teacher t on s.tid=t.id

或者select * from student s left join teacher t on s.tid(+)=t.id

id name tid id name

1 張三 1 1 ***

2 李四 2 2 張老師

3 王五 1 1 ***

3 鄭老師

4、全連線

with student as(

select '1' id,'張三' name,'1' tid from dual union all

select '2' id,'李四' name,'2' tid from dual union all

select '3' id,'王五' name,'1' tid from dual union all

select '4' id,'趙六' name,'5' tid from dual

),teacher as(

select '1' id,'***' name from dual union all

select '2' id,'張老師' name from dual union all

select '3' id,'鄭老師' name from dual

)select * from student s full join teacher t on s.tid=t.id

id name tid id name

1 張三 1 1 ***

2 李四 2 2 張老師

3 王五 1 1 ***

4 趙六 5

3 鄭老師

總結:where條件和內連線查詢出來的資料是一樣的,只查詢出來能關聯上的資料

外連線 是按照主表的資料多少展現出來的。如左外連線主表就是student;右外連 接主表就是teacher

全連線 是把所有的資料都展現出來、不管有沒有關聯出來資料

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

現有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 189...

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

1.內連線我們通常用的連線,表表連線只顯示交集資料 2.外連線分左外連線 table1 left outer join on table2 和右外連線table1 right outer join on table2 和全連線 table1 full outer join on table2 2.1...

MySQL內連線 左外連線 右外連線 全外連線

連線的優勢是減少了外來鍵的使用。內連線 最常見的等值連線,指連線結果僅包含符合連線條件的行,參與連線的兩個表都應該符合連線條件。inner join或join on condition 首先建立兩個表person和card,內容如下 select from card id cardname 1 ca...