在项目中用到多表联合查询,发现2个现象:
1、left join连接2张表,on后的条件第一个生效,用and连接的其他条件不生效;
2、一旦加上where,则显示的结果等同于inner join;
分析结果:
过滤条件放在:
- where后面:是先连接然生成临时查询结果,然后再筛选
- on后面:先根据条件过滤筛选,再连 生成临时查询结果
table1 left join table2 on 条件1(有效) and 条件2(无效)
table1 left join table2 on 条件1(有效) where 条件2(有效)
table1 left join table2 on (条件1(有效) and 条件2(有效))