聯結的語法:mysql
... from table1 inner|left|right join table2 on conditionsql
內外聯結的區別: 內聯結將去除全部不符合條件condition的記錄,外聯結將保留部分不符合condition的記錄;性能優化
左聯結將保留左邊表table1的記錄,此時右邊表table2只返回符合condition的記錄。性能
1,join概述優化
... from table1 inner|left|right join table2 on conditionblog
inner join : 內聯結,等值聯結,取得兩個表中符合condition的記錄id。it
left join : 左聯結,取得table1的全部記錄(table1中condition中的字段可能爲空),和table2符合condition的記錄,io
right join : 右聯結,取得table2的全部的記錄(table2中condition中的字段可能爲空),和table1符合condition的記錄,table
2,Inner joinmobile
內聯結
select * from A inner join B on A.mobile = B.mobile and andCondition;
將會返回 A.id 不爲空,B.id 不爲空,且A.mobile = B.mobile 和符合 andCondition的數據
3,left join
select * from A left join B on A.mobile = B.mobile and andCondition;
將會返回A的全部記錄和 B.mobile = A.mobile的全部B的記錄。
若是想取得A表中不知足condition的數據
select * from A
left join B on A.mobile = B.mobile
where B.is is null
獲得
用left join 模擬 inner join
-> select * from A left join B on A.mobile = B.mobile where B.id is not null;
求A B 表中不符合condition條件的各自數據的集合
-> select * from A left join B on A.mobile = B.mobile where B.id is null
union
select * from A right join B on A.mobile = B.mobile where A.id is null
獲得差別數據(不符合condition的兩個表的數據)
4,right join
-> select * from A right B on A.mobile = B.mobile ;
將獲得B表的全部數據和A表中知足condition的數據
5,cross join
交叉聯結,獲得的是兩個表的乘積
在mysql中(僅限mysql) cross join 和 inner join 的表現是同樣的。在不指定on條件獲得的都是笛卡爾積。
因此下面的三個語句效果同樣
->...from A inner join B
->...from A cross join B
->...from A join B
6,full join
-> select * from A left join B on A.mobile = B.mobile;
union
select * from A right join B on A.mobile = B.mobile;
獲得
7,性能優化
(1)顯示inner join 和 隱式inner join
顯示 --> select * from A inner join B on A.mobile = B.mobile;
隱式 --> select * from A inner join B where A.mobile = B.mobile;
10萬數據的查詢用時幾乎相等。
(2)left join / right join 和 inner join
儘可能用inner join 避免 外聯結 和 null
在使用外聯結的時候,如 -> select * from A left join B on A.mobile = B.mobile where whereCondition;
若是B中沒有知足on condition的條件,則會產生一行全部列爲null的數據。
在 on condition 匹配階段,where 條件不會被使用。在on condition結束後,where將會被使用,where條件將會從知足on condition的數據中再檢索一次。
因此,在使用外聯結市,咱們要儘可能給出儘量多的匹配知足條件(即 on condition),減小where字句的檢索。
不建議sql -> select * from A
left join B on A.mobile = B.mobile
left join C on A.name = C.name
where A.status = 1 and C.status = 1
建議的sql -> select * from A
left join B on A.mobile = B.mobile and A.status = 1
left join C on A.name = C.name and C.status = 1
儘可能知足on condition,而少使用where的條件。
(3)on 條件 和 where 條件的不一樣
->select * from A left join B on A.mobile = B.mobile on A.name is not null;
將會返回A表的全部記錄 和 B表中知足 (A.mobile = B.mobile on A.name is not null) 的記錄;
->select * from A left join B on A.mobile = B.mobile where A.name is not null;
將會返回A表中全部記錄 和 B表中知足 (A.mobile = B.mobile)的記錄,而後 再經過where條件(A.name is not null)對結果進行篩選。
第一條sql語句返回的結果集條數 >= 第二條sql
(4)儘可能避免子查詢,而用join