left join on 后面接and条件 和 where 后面接and条件 区别

inner join,left join,right,join 三种表连接的方式区别自行查找,或者可以参考链接:https://www.cnblogs.com/FENGXUUEILIN/p/10039699.html

最近在完成一个查询时,遇到这个问题,记录一下。
两张测试表:
product表
left join on 后面接and条件 和 where 后面接and条件 区别_第1张图片
product_detail表
left join on 后面接and条件 和 where 后面接and条件 区别_第2张图片
sql1:

`select * from product a left join product_detail b on a.id = b.id and b.weight != 44 and b.exist = 0;`

结果1:
left join on 后面接and条件 和 where 后面接and条件 区别_第3张图片
sql2:

select * from product a left join product_detail b on a.id = b.id where  b.weight != 44 and b.exist = 0;

结果2:
在这里插入图片描述
on 条件 用来决定如何从子表(这里的子表是b)中检索数据行。
如果子表中没有任何一行数据匹配 on 的条件,将会额外生成一行所有列为 NULL 的数据。
如果字表中有匹配on的条件,但是被on后面的and条件过滤,此时这一行还是为null,就如sql1中and把b表中id为4的过滤掉。

在匹配阶段 where子句的条件都不会被使用。仅在匹配阶段完成以后,wehre子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤。如sql2。

你可能感兴趣的:(left join on 后面接and条件 和 where 后面接and条件 区别)