標籤:
首先借用官方的解釋下:
inner join(等值串連):只返回兩個表中連接欄位相等的行;
left join(左聯結):返回包括左表中的所有記錄和右表中連接欄位相等的記錄;
right join(右聯結):返回包括右表中的所有記錄和左表中連接欄位相等的記錄。
比如我們有xs、cj兩個表
xs表 cj表
--------------- ----------------------
id name id score
1 張三 1 96
2 李四 2 80
3 86
Sql代碼
1 SELECT * FROM `xs` INNER JOIN `cj` ON xs.id = cj.id
返回
------------------------
id name id score
1 張三 1 96
2 李四 2 80
-----------------------
Sql代碼
2 SELECT * FROM `xs` LEFT JOIN `cj` ON xs.id = cj.id
返回
------------------------
id name id score
1 張三 1 96
2 李四 2 80
-----------------------
Sql代碼
3 SELECT * FROM `xs` RIGHT JOIN `cj` ON xs.id = cj.id
返回
id name id score
1 張三 1 96
2 李四 2 80
NULL NULL 3 86
其中還有inner join還有另外一種寫法,兩者是等價的,都是等值串連
Sql代碼
4 SELECT * FROM `xs`,`cj` WHERE xs.id = cj.id
mysql多表串連查詢inner join, left join , right join ,full join ,cross join
關鍵字: mysql inner join left join right join full join cross join
inner join,full outer join,left join,right jion
內部串連 inner join 兩表都滿足的組合
full outer 全連 兩表相同的組合在一起,A表有,B表沒有的資料(顯示為null),同樣B表有
A表沒有的顯示為(null)
A表 left join B表 左連,以A表為基礎,A表的全部資料,B表有的組合。沒有的為null
A表 right join B表 右連,以B表為基礎,B表的全部資料,A表的有的組合。沒有的為null
查詢分析器中執行:
--建表table1,table2:
create table table1(id int,name varchar(10))
create table table2(id int,score int)
insert into table1 select 1,‘lee‘
insert into table1 select 2,‘zhang‘
insert into table1 select 4,‘wang‘
insert into table2 select 1,90
insert into table2 select 2,100
insert into table2 select 3,70
如表
-------------------------------------------------
table1|table2|
-------------------------------------------------
idname|idscore|
1lee|190|
2zhang|2100|
4wang|370|
-------------------------------------------------
以下均在查詢分析器中執行
一、外串連
1.概念:包括左向外聯結、右向外聯結或完整外部聯結
2.左串連:left join 或 left outer join
(1)左向外聯結的結果集包括 LEFT OUTER 子句中指定的左表的所有行,而不僅僅是聯結列所匹配的行。如果左表的某行在右表中沒有匹配行,則在相關聯的結果集行中右表的所有挑選清單列均為空白值(null)。
(2)sql 語句
select * from table1 left join table2 on table1.id=table2.id
-------------結果-------------
idnameidscore
------------------------------
1lee190
2zhang2100
4wangNULLNULL
------------------------------
注釋:包含table1的所有子句,根據指定條件返回table2相應的欄位,不符合的以null顯示
3.右串連:right join 或 right outer join
(1)右向外聯結是左向外聯結的反向聯結。將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。
(2)sql 語句
select * from table1 right join table2 on table1.id=table2.id
-------------結果-------------
idnameidscore
------------------------------
1lee190
2zhang2100
NULLNULL370
------------------------------
注釋:包含table2的所有子句,根據指定條件返回table1相應的欄位,不符合的以null顯示
4.完整外部聯結:full join 或 full outer join
(1)完整外部聯結返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的挑選清單列包含空值。如果表之間有匹配行,則整個結果集行包含基表的資料值。
(2)sql 語句
select * from table1 full join table2 on table1.id=table2.id
-------------結果-------------
idnameidscore
------------------------------
1lee190
2zhang2100
4wangNULLNULL
NULLNULL370
------------------------------
注釋:返回左右串連的和(見上左、右串連)
二、內串連
1.概念:內聯結是用比較子比較要聯結列的值的聯結
2.內串連:join 或 inner join
3.sql 語句
select * from table1 join table2 on table1.id=table2.id
-------------結果-------------
idnameidscore
------------------------------
1lee190
2zhang2100
------------------------------
注釋:只返回合格table1和table2的列
4.等價(與下列執行效果相同)
A:select a.*,b.* from table1 a,table2 b where a.id=b.id
B:select * from table1 cross join table2 where table1.id=table2.id (註:cross join後加條件只能用where,不能用on)
三、交叉串連(完全)
1.概念:沒有 WHERE 子句的交叉聯結將產生聯結所涉及的表的笛卡爾積。第一個表的行數乘以第二個表的行數等於笛卡爾積結果集的大小。(table1和table2交叉串連產生3*3=9條記錄)
2.交叉串連:cross join (不帶條件where...)
3.sql語句
select * from table1 cross join table2
-------------結果-------------
idnameidscore
------------------------------
1lee190
2zhang190
4wang190
1lee2100
2zhang2100
4wang2100
1lee370
2zhang370
4wang370
------------------------------
注釋:返回3*3=9條記錄,即笛卡爾積
4.等價(與下列執行效果相同)
A:select * from table1,table2
【轉】Mysql之inner join,left join,right join詳解