sofast 2019-06-27
表1:test_user
表2:
一:LEFT JOIN
1、SELECT * from test_user a LEFT JOIN test_order b ON a.name
=b.name
2、SELECT * from test_user a LEFT JOIN test_order b ON a.name
=b.name
where a.name
='张三'
3、SELECT * from test_user a LEFT JOIN test_order b ON a.name
=b.name
and a.name
='张三'
4、SELECT * from test_user a LEFT JOIN test_order b ON a.name
=b.name
where b.book
='热血传奇'
5、SELECT * from test_user a LEFT JOIN test_order b ON a.name
=b.name
and b.book
='热血传奇'
结论:以上的结果可以得出,条件写在where 和 on 中的区别在于,左连接查询的特性,不管右边是否存在匹配数据,左边的数据全部查出来。
二:RIGHT JOIN
结论:和LEFT JOIN差不多,右连接是不管左边是否存在匹配数据,右边的数据全部查出来。
三、INNER JOIN
1、SELECT * from test_user a INNER JOIN test_order b ON a.name
=b.name
WHERE b.book
='热血传奇'
2、SELECT * from test_user a INNER JOIN test_order b ON a.name
=b.name
and b.book
='热血传奇'
3、等价于:SELECT * from test_user a , test_order b where a.name
=b.name
and b.book
='热血传奇'
结论:从上面测试可以看出在内连接中条件写在where 和 on 中 结果没有差别,但是效率在理论上写在on 中要比写在where 中效率高,因为写在on中是先筛选再连接,效率上比先连接再筛选要高。
效率:
1、inner join比left join快
2、inner join 条件写在on中比写在where中快