解析sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別
更新時間:2013年07月17日 11:43:23 作者:
以下是對在sql語句中l(wèi)eft_join、inner_join中的on與where的區(qū)別進行了詳細(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 后面的條件對它不起作用,除非再后面再加上where來進行篩選,這就是sql語句2了;由sql語句3可見,on后面的條件中,右表的限制條件將會起作用。
**************************************************************************
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確實能得到相同的結(jié)果,只是效率不同(這個我沒有測試過,不過我相信這個結(jié)論)。
但是sql語句6是否比sql語句7的效率要低一些,我沒有足夠的數(shù)據(jù)量來測試,不過我也相信是如此的。
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 后面的條件對它不起作用,除非再后面再加上where來進行篩選,這就是sql語句2了;由sql語句3可見,on后面的條件中,右表的限制條件將會起作用。
**************************************************************************
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確實能得到相同的結(jié)果,只是效率不同(這個我沒有測試過,不過我相信這個結(jié)論)。
但是sql語句6是否比sql語句7的效率要低一些,我沒有足夠的數(shù)據(jù)量來測試,不過我也相信是如此的。
相關(guān)文章
cmd中MySQL中文數(shù)據(jù)亂碼問題解決方法
MySQL是默認(rèn)utf8編碼的,所建數(shù)據(jù)庫也是設(shè)置utf8編碼,使用程序可以新增中文數(shù)據(jù),在cmd中使用SQL語句新增數(shù)據(jù)則報錯,有類似情況的朋友可以參考下本文2014-02-02MySQL數(shù)據(jù)庫之聯(lián)合查詢?union
這篇文章主要介紹了MySQL數(shù)據(jù)庫之聯(lián)合查詢?union,聯(lián)合查詢就是將多個查詢結(jié)果的結(jié)果集合并到一起,字段數(shù)不變,多個查詢結(jié)果的記錄數(shù)合并,下文詳細(xì)介紹需要的小伙伴可以參考一下2022-06-06學(xué)習(xí)mysql之后的一點總結(jié)(基礎(chǔ))
學(xué)習(xí)mysql之后的一點總結(jié),比較適合新手朋友2012-05-05Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉(zhuǎn)換問題
這篇文章主要介紹了Mysql中in和exists的區(qū)別?&?not?in、not?exists、left?join的相互轉(zhuǎn)換,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09