join中on和where的区别
2016-01-18 15:36
344 查看
table a(id, type):
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql语句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql语句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql语句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql语句1的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql语句2的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql语句3的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql语句1可见,left join 中左表的全部记录将全部被查询显示,on 后面的条件对它不起作用,除非再后面再加上where来进行筛选,这就是sql语句2了;由sql语句3可见,on后面的条件中,右表的限制条件将会起作用。
**********************************************************************************
sql语句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql语句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql语句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql语句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
这四条语句的执行结果一样,如下:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
由此可见,inner join 中on后面的限制条件将全部起作用,这与where的执行结果是一样的。另外,where语句与inner join确实能得到相同的结果,只是效率不同(这个我没有测试过,不过我相信这个结论)。
但是sql语句6是否比sql语句7的效率要低一些,我没有足够的数据量来测试,不过我也相信是如此的。
id type
----------------------------------
1 1
2 1
3 2
table b(id, class):
id class
---------------------------------
1 1
2 2
sql语句1:select a.*, b.* from a left join b on a.id = b.id and a.type = 1;
sql语句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;
sql语句3:select a.*, b.* from a left join b on a.id = b.id and b.class = 1;
sql语句1的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
3 2
sql语句2的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
sql语句3的执行结果为:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1
3 2
由sql语句1可见,left join 中左表的全部记录将全部被查询显示,on 后面的条件对它不起作用,除非再后面再加上where来进行筛选,这就是sql语句2了;由sql语句3可见,on后面的条件中,右表的限制条件将会起作用。
**********************************************************************************
sql语句4:select a.*, b.* from a inner join b on a.id = b.id and a.type = 1;
sql语句5:select a.*, b.* from a inner join b on a.id = b.id where a.type = 1;
sql语句6:select a.*, b.* from a, b where a.id = b.id and a.type = 1;
sql语句7:select a.*, b.* from a, b where a.type = 1 and a.id = b.id;
这四条语句的执行结果一样,如下:
a.id a.type b.id b.class
----------------------------------------
1 1 1 1
2 1 2 2
由此可见,inner join 中on后面的限制条件将全部起作用,这与where的执行结果是一样的。另外,where语句与inner join确实能得到相同的结果,只是效率不同(这个我没有测试过,不过我相信这个结论)。
但是sql语句6是否比sql语句7的效率要低一些,我没有足够的数据量来测试,不过我也相信是如此的。
相关文章推荐
- Infinispan-Redis配置使用
- XML基础
- XML基础
- Xamarin应用商店最受欢迎的10款免费用户界面工具
- leetcode总结-- 排序算法(内部排序)
- AndroidJNI 通过C/C++调用JAVA
- Bootstrap面包屑导航、分页、标签、徽章
- java通用日期转换(string 转 date)
- Spring配置文件中的 default-lazy-init="true"
- Linux meld安装
- 【人脸识别】初识人脸识别
- Prison Jamming system remote control by PC
- PCFI
- java入门学习(10)—抽象类
- <context:component-scan>使用说明
- I/O端口的访问
- Java细节之static方法重写
- ListView布局之View复用原理举例
- 已经完成的习惯
- 读取固定ResourceBundle