join的写法:
1)如果使用left join,左边的表不一定是驱动表;
2)如果业务逻辑明确要求使用left join,则不能把被驱动表的字段放在where后面做等值或不等值判断,必须都写在on后面,否则优化器就会转换,使用join...where的语法进行改写;
select * from a left join b on(a.f1=b.f1) and (a.f2=b.f2);
3)如果使用join...on,谓词条件无论是在on还是在where,语义逻辑是等价的。
select * from a join b on(a.f1=b.f1) and (a.f2=b.f2);
select * from a join b on(a.f1=b.f1) where (a.f2=b.f2);
注:1)对于inner join,两表的谓词条件无论放在on,还是放在where,它们都是等价的;
2)对于left join,左表的谓词条件放在on不会对左表数据进行过滤,依然显示左表全部数据,放在where才会对左表进行过滤,右表谓词条件无论放在on,还是where,都会对右表先过滤再连接,但是放在where,left join会转换为inner join。