日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

sql語句中l(wèi)eft join、inner join中的on與where的區(qū)別

 jlqin717 2010-10-10

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來進(jìn)行篩選,這就是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ù)量來測試,不過我也相信是如此的。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多