今天看到有人提问,问join on后边加and和 where后边加and有什么区别,因此来做一个案例有助于理解。
首先准备数据,test1,test2数据如下:
1、left join and和 left join where and
由以上四组查询可以得出以下结论:
1、当条件加在join on之后,则只会影响右边数据,不会影响左表数据,不管条件是否成立,左边数据都能正常返回;
2、当条件加在where 之后则表示对关联结果再进行筛选,此时的结果将会依赖于where后边条件的真假
2、inner join and和 inner join where and
由以上四组查询得出:inner join and 和 inner join where and 其查询结果基本一致
3、full join and 和 full join where and
由以上查询可以得出:
1、当条件加在full on之后,则会根据and之后的条件分别影响左表或右表数据,
2、当条件加在where 之后则表示对关联结果再进行筛选,此时的结果将会依赖于where后边条件的真假。
总结:
1、当条件加在 left(right) join on之后,则只会影响右(左)边数据,不会影响左(右)表数据,不管条件是否成立,左(右))边数据都能正常返回;
2、inner join and 和 inner join where and 其查询结果基本一致;
3、当条件加在full on之后,则会根据and之后的条件分别影响左表或右表数据;
2、当条件加在where 之后则表示对关联结果再进行筛选,此时的结果将会依赖于where后边条件的真假