解析sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別
更新時(shí)間:2013年07月17日 11:43:23 作者:
以下是對(duì)在sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
table a(id, type):
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql語句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql語句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql語句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql語句1的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql語句2的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql語句3的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql語句1可見,left join 中左表的全部記錄將全部被查詢顯示,on 后面的條件對(duì)它不起作用,除非再后面再加上where來進(jìn)行篩選,這就是sql語句2了;由sql語句3可見,on后面的條件中,右表的限制條件將會(huì)起作用。
**************************************************************************
sql語句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql語句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql語句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql語句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
這四條語句的執(zhí)行結(jié)果一樣,如下:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
由此可見,inner join 中on后面的限制條件將全部起作用,這與where的執(zhí)行結(jié)果是一樣的。另外,where語句與inner join確實(shí)能得到相同的結(jié)果,只是效率不同(這個(gè)我沒有測(cè)試過,不過我相信這個(gè)結(jié)論)。
但是sql語句6是否比sql語句7的效率要低一些,我沒有足夠的數(shù)據(jù)量來測(cè)試,不過我也相信是如此的。
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql語句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql語句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql語句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql語句1的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql語句2的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql語句3的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql語句1可見,left join 中左表的全部記錄將全部被查詢顯示,on 后面的條件對(duì)它不起作用,除非再后面再加上where來進(jìn)行篩選,這就是sql語句2了;由sql語句3可見,on后面的條件中,右表的限制條件將會(huì)起作用。
**************************************************************************
sql語句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql語句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql語句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql語句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
這四條語句的執(zhí)行結(jié)果一樣,如下:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
由此可見,inner join 中on后面的限制條件將全部起作用,這與where的執(zhí)行結(jié)果是一樣的。另外,where語句與inner join確實(shí)能得到相同的結(jié)果,只是效率不同(這個(gè)我沒有測(cè)試過,不過我相信這個(gè)結(jié)論)。
但是sql語句6是否比sql語句7的效率要低一些,我沒有足夠的數(shù)據(jù)量來測(cè)試,不過我也相信是如此的。
相關(guān)文章
MYSQL 的10大經(jīng)典優(yōu)化案例場(chǎng)景實(shí)戰(zhàn)
在應(yīng)用開發(fā)的早期,數(shù)據(jù)量少,開發(fā)人員開發(fā)功能時(shí)更重視功能上的實(shí)現(xiàn),隨著生產(chǎn)數(shù)據(jù)的增長(zhǎng),很多SQL語句開始暴露出性能問題,對(duì)生產(chǎn)的影響也越來越大,有時(shí)可能這些有問題的SQL就是整個(gè)系統(tǒng)性能的瓶頸。本文我們就聊聊MYSQL的優(yōu)化2021-09-09
MySQL中Next-Key Lock底層原理實(shí)現(xiàn)
Next-KeyLock是MySQL InnoDB存儲(chǔ)引擎中的一種鎖機(jī)制,結(jié)合記錄鎖和間隙鎖,用于高效并發(fā)控制并避免幻讀,本文主要介紹了MySQL中Next-Key Lock底層原理實(shí)現(xiàn),感興趣的可以了解一下2025-03-03
安裝配置MySQLMTOP來監(jiān)控MySQL運(yùn)行性能的教程
這篇文章主要介紹了安裝配置MySQLMTOP來監(jiān)控MySQL運(yùn)行性能的教程,MySQLMTOP具有B/S方式的圖形化操作頁面,需要的朋友可以參考下2015-12-12
mysql使用物理備份安裝xtrabackup的詳細(xì)過程
這篇文章主要介紹了mysql使用物理備份安裝xtrabackup的詳細(xì)過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
mysql SELECT語句去除某個(gè)字段的重復(fù)信息
mysql SELECT語句去除某個(gè)字段的重復(fù)信息,需要的朋友可以收藏下。2010-04-04

