table a(id, type):
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql語(yǔ)句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql語(yǔ)句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql語(yǔ)句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql語(yǔ)句1的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql語(yǔ)句2的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql語(yǔ)句3的執(zhí)行結(jié)果為:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql語(yǔ)句1可見(jiàn),left join 中左表的全部記錄將全部被查詢顯示,on 后面的條件對(duì)它不起作用,除非再后面再加上where來(lái)進(jìn)行篩選,這就是sql語(yǔ)句2了;由sql語(yǔ)句3可見(jiàn),on后面的條件中,右表的限制條件將會(huì)起作用。
**************************************************************************
sql語(yǔ)句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql語(yǔ)句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql語(yǔ)句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql語(yǔ)句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;