left join on and 与 left join on where的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
在使用left jion时,on和where条件的区别如下:
1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
假设有两张表:
表1 tab1:
id size
1 10
2 20
3 30
表2 tab2:
size name
10 AAA
20 BBB
20 CCC
两条SQL:
1、select * formtab1 left join tab2 on (tab1.size = tab2.size) where tab2.name=’AAA’
2、select * formtab1 left join tab2 on (tab1.size = tab2.size and tab2.name=’AAA’)
第一条SQL的过程:
1、中间表
on条件:
tab1.size = tab2.size
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 20 BBB
2 20 20 CCC
3 30 (null) (null)
2、再对中间表过滤
where 条件:
tab2.name=’AAA’
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
第二条SQL的过程:
1、中间表
on条件:
tab1.size = tab2.size and tab2.name=’AAA’
(条件不为真也会返回左表中的记录)
tab1.id tab1.size tab2.size tab2.name
1 10 10 AAA
2 20 (null) (null)
3 30 (null) (null)
其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。而inner
jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。
mysql> select * from aaa a left join bbb b on a.id = b.id and b.name = '111111111';
+------+-----------+------+-----------+
| id | name | id | name |
+------+-----------+------+-----------+
| 1 | 陈亮亮 | 1 | 111111111 |
| 1 | 福特 | 1 | 111111111 |
| 1 | bentian | 1 | 111111111 |
| 2 | 张云霞 | NULL | NULL |
| 3 | 陈小平 | NULL | NULL |
| 4 | 陈小肥 | NULL | NULL |
| 2 | 宝马 | NULL | NULL |
| 3 | 奔驰 | NULL | NULL |
| 4 | 奥迪 | NULL | NULL |
| 2 | dazong | NULL | NULL |
| 5 | ben | NULL | NULL |
| 6 | da | NULL | NULL |
+------+-----------+------+-----------+
12 rows in set (0.01 sec)
mysql> select * from aaa a left join bbb b on a.id = b.id where b.name = '111111111';
+------+-----------+------+-----------+
| id | name | id | name |
+------+-----------+------+-----------+
| 1 | 陈亮亮 | 1 | 111111111 |
| 1 | 福特 | 1 | 111111111 |
| 1 | bentian | 1 | 111111111 |
+------+-----------+------+-----------+
3 rows in set (0.00 sec)
mysql> select * from aaa a left join bbb b on a.id = b.id where a.name = 'bentian';
+------+---------+------+-----------+
| id | name | id | name |
+------+---------+------+-----------+
| 1 | bentian | 1 | 111111111 |
+------+---------+------+-----------+
1 row in set (0.00 sec)
mysql> select * from aaa;
+------+-----------+
| id | name |
+------+-----------+
| 1 | 陈亮亮 |
| 2 | 张云霞 |
| 3 | 陈小平 |
| 4 | 陈小肥 |
| 1 | 福特 |
| 2 | 宝马 |
| 3 | 奔驰 |
| 4 | 奥迪 |
| 1 | bentian |
| 2 | dazong |
| 5 | ben |
| 6 | da |
+------+-----------+
12 rows in set (0.02 sec)
mysql> select * from bbb;
+------+------------+
| id | name |
+------+------------+
| 1 | 111111111 |
| 2 | 2222222222 |
| 3 | 3333333333 |
+------+------------+
left join on and 与 left join on where的区别的更多相关文章
- 数据库中的左连接(left join)和右连接(right join)区别
Left Join / Right Join /inner join相关 关于左连接和右连接总结性的一句话: 左连接where只影向右表,右连接where只影响左表. Left Join select ...
- MySQL的JOIN(四):JOIN优化实践之快速匹配
这篇博文讲述如何优化扫描速度.我们通过MySQL的JOIN(二):JOIN原理得知了两张表的JOIN操作就是不断从驱动表中取出记录,然后查找出被驱动表中与之匹配的记录并连接.这个过程的实质就是查询操作 ...
- MySQL的JOIN(五):JOIN优化实践之排序
这篇博文讲述如何优化JOIN查询带有排序的情况.大致分为对连接属性排序和对非连接属性排序两种情况.插入测试数据. CREATE TABLE t1 ( id INT PRIMARY KEY AUTO_I ...
- Python3 join函数和os.path.join用法
Python3 join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...
- Python join() 方法与os.path.join()的区别
Python join() 方法与os.path.join()的区别 pythonJoinos.path.join 今天工作中用到python的join方法,有点分不太清楚join() 方法与os.p ...
- “,”、“natural join”、“natural left outer join”、“natural right outer join”的用法总结
“,”:代表笛卡尔积: “natural join”:代表自然连接,即同名列等值连接: “natural left outer join”:表示左外连接: “natural right outer j ...
- hive 包含操作(left semi join)(left outer join = in)迪卡尔积
目前hive不支持 in或not in 中包含查询子句的语法,所以只能通过left join实现. 假设有一个登陆表login(当天登陆记录,只有一个uid),和一个用户注册表regusers(当天注 ...
- 【Transact-SQL】SQL Server自动把left join自动转化为inner join、以及关联时的数据重复问题
原文:[Transact-SQL]SQL Server自动把left join自动转化为inner join.以及关联时的数据重复问题 1.SQL Server自动把left join自动转化为inn ...
- left join on and和left join on where条件的困惑[转]
外连接:left join(左联接) left outer join 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) right outer join返回包括右表中的 ...
- 转!!left join on and 与 left join on where的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时 ...
随机推荐
- springboot- logback 可切换不同环境
在resources下新建一个logback文件夹和一个logback.xml logback.xml <?xml version="1.0" encoding=" ...
- 精心收集的 95 个超实用的 JavaScript 代码片段( ES6+ 编写)
https://www.html.cn/archives/8748#table-of-contents https://www.haorooms.com/post/js_regexp
- ESXI 6.5安装详细步骤
网址:http://blog.51cto.com/laotang6/2044861 ESXi是专为运行虚拟机.最大限度降低配置要求和简化部署而设计.只需几分钟时间,客户便可完成从安装到运行虚拟机的全过 ...
- Codeforces Round #513 总结
首次正式的$Codeforces$比赛啊,虽然滚粗了,然而终于有$rating$了…… #A Phone Numbers 签到题,然而我第一次写挂了(因为把11看成8了……) 只需要判断一下有多少个 ...
- Magento 2 Plugin - Interceptor - Magento 2插件 - 拦截器-插件开发
Magento 2 Plugin - Interceptor - Magento 2插件 - 拦截器 Magento 2 Plugin is a technical plugin for your b ...
- LOJ #2533. 「CTSC2018」暴力写挂(边分治合并)
题意 给你两个有 \(n\) 个点的树 \(T, T'\) ,求一对点对 \((x, y)\) 使得 \[ depth(x) + depth(y) - (depth(LCA(x , y)) + dep ...
- Oracle 查看链接数、创建索引等的DDL语句
select count(*),machine from v$session group by machine 今天打算将一个数据库的索引在另一个测试库上重新创建一遍,研究了一下. set pages ...
- LFYZ-OJ ID: 1009 阶乘和
思路 循环n次,每次计算i的阶乘i!,并加入sum中. n的范围从1~100,这里一定要使用高精度运算,涉及到"高精度乘低精度","高精度加高精度". 避免每次 ...
- 第九节:深究并行编程Parallel类中的三大方法 (For、ForEach、Invoke)和几大编程模型(SPM、APM、EAP、TAP)
一. 并行编程 1. 区分串行编程和串行编程 ①. 串行编程:所谓的串行编程就是单线程的作用下,按顺序执行.(典型代表for循环 下面例子从1-100按顺序执行) ②. 并行编程:充分利用多核cpu的 ...
- 前端面试题整理—JavaScript篇(二)
1.使用js实现一个可持续的动画 2.实现一个可以自由拖动的悬浮框 3.实现一个倒计时效果 4.使用js仿写一个原生下拉列表框 5.创建10个<a>标签,点击的时候弹出对应的序号 6.实现 ...