SQL內連線與外連線的區別

2021-06-22 06:05:14 字數 1473 閱讀 7087

你是要弄清楚區別在什麼地方還是單純想要文字說明 

文字說明的樓上說了一大堆了,不說了。 

弄個例題,直觀一點。兩個表: 

--表stu 

id name 

1, jack 

2, tom 

3, kity 

4, nono 

--表exam 

id grade 

1, 56 

2, 76 

11, 89 

內連線 (顯示兩表id匹配的) 

select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id 

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

1 1 jack 56 

2 2 tom 76 

左連線(顯示join 左邊的表的所有資料,exam只有兩條記錄,所以stu.id,grade 都用null 顯示) 

select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id 

1 1 jack 56 

2 2 tom 76 

3 null kity null 

4 null nono null 

右連線(與作連線相反,顯示join右邊表的所有資料) 

select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id 

1 1 jack 56 

2 2 tom 76 

null 11 null 89

內連線取交集,外連線分左和右, 

左連線左邊的全取, 

右連線右邊的全取

內連線:進行連線的兩個表對應的相匹配的字段完全相同的連線。 

外連線又分為左外連線和右外連線。 

左連線即left outer join: 

兩個表進行左連線時會返回左邊表中的所有的行和右邊表中與之相匹配的列值沒有相匹配的用空值代替。 

右連線即right outer join: 

兩個表進行右連線時會返回右邊表中的所有的行和左邊表中與之相匹配的列值沒有相匹配的用空值代替。 

能看明白嗎。。。

內聯接:顯示兩個表想匹配的行,左連線顯示jion左表的行,右表沒有想匹配的,用nul代替!右聯接和左連線相反,全連線和左右連線的合計。 

使用左向外聯接 

假設在 city 列上聯接 authors 表和 publishers 表。結果只顯示在出版商所在城市居住的作者(本例中為 abraham bennet 和 cheryl carson)。 

若要在結果中包括所有的作者,而不管出版商是否住在同乙個城市,請使用 sql-92 左向外聯接。下面是 transact-sql 左向外聯接的查詢和結果:

SQL內連線與外連線的區別

表stu id name 1,jack 2,tom 3,kity 4,nono 表exam id grade 1,56 2,76 11,89 內連線 顯示兩表id匹配的 select stu.id,exam.id,stu.name,exam.grade from stu inner join exa...

SQL的內連線與外連線

在oracle的sql語句常用的連線有內連線 inner join 外連線 outer join 等,內連線又包括等值連線,非等值連線,自連線 而外連線又分為左連線和右連線。其中預設的是內連線的等值連線。為了方便我們建立兩張最簡易的表a b,具體的表結構參看下面,來分析內連線與外連線的區別 圖1 圖...

SQL的內連線與外連線

有兩個表a和表b。表a結構如下 aid int 標識種子,主鍵,自增id aname varchar 資料情況,即用select from a出來的記錄情況如下圖1所示 表b結構如下 bid int 標識種子,主鍵,自增id bnameid int 資料情況,即用select from b出來的記錄...