left/right/inner/all join以及 on后面的查询条件

版权声明:转载请取得博主同意 https://blog.csdn.net/weixin_43050247/article/details/89388561

1.on后面没有多余查询条件

当查询a表b表是,on后面只有一个连接条件:a.feild=b.field

  • left join on
    以左表(a)为主,查询出所有的a表数据,匹配不上的b表数据置为null
  • right join on
    以右表(b)为主,查询出所有的b表数据,匹配不上的a表数据置为null
  • inner join on
    只保留两张表(a,b)的完全匹配结果
  • all join on
    保留所有的结果,匹配不上的a,b表数据都会置为null

2.匹配条件放在(left join) on 和where后面的区别

参考链接:https://blog.csdn.net/czhphp/article/details/18657341

  • on条件用来决定如何从B表中检索数据行,如果B表中没有一行数据符合,将会生成一行所有列均为null的数据
  • 匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE 子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤
 SELECT a.*,b.* FROM product a LEFT JOIN product_details b ON a.id=b.id AND b.weight = 44 ;

在这里插入图片描述

 SELECT a.*,b.* FROM product a LEFT JOIN product_details b ON a.id=b.id where b.weight = 44 ;

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43050247/article/details/89388561