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条件是在生成临时 ...
随机推荐
- U66785 行列式求值
二更:把更多的行列式有关内容加了进来(%%%%%Jelly Goat奆佬) 题目描述 给你一个N(n≤10n\leq 10n≤10)阶行列式,请计算出它的值 输入输出格式 输入格式: 第一行有一个整数 ...
- 读Zepto源码之内部方法
数组方法 定义 var emptyArray = [] concat = emptyArray.concat filter = emptyArray.filter slice = emptyArray ...
- 【洛谷P1134 阶乘问题】
[传送门] #include<bits/stdc++.h> using namespace std; int main() { ; cin>>a; ;i<=a;i++) ...
- SEO学习知识
监控流量的工具 百度统计 CNZZ 51LA 谷歌分析工具 如何从平台借流量? 竞价(付费).SEO 关键词定位: 定位人:负责人 将公司的业务全部列出来 选词: 根据定位的关键词选择出我们需要优化 ...
- 关于设计项目UI界面的软件工具
关于画UI界面的软件,我在网上找了几个,今天式用这几款软件还可以 1.墨刀:国产的,这个专门画APP界面的,用起来比较简单,有免费版的,要注册才能用,提供云存储,收费版的云存储空间会多一些.网站: h ...
- 使用InternalsVisibleTo给assembly添加“友元assembly”
C#的internal关键字可以使标记的方法,字段或者属性等等只能在当前assembly内部使用,那么如果其他的assembly需要使用这个internal的方法的时候怎么办呢?.NET提供了一种类似 ...
- free命令查看内存信息
free介绍 FREE(1) Linux User’s Manual FREE(1) NAME free - Display amount of free and used memory in the ...
- SSH HTTP代理
SSH 连接 参照https://stackoverflow.com/questions/19161960/connect-with-ssh-through-a-proxy 若要使用goflyway连 ...
- 关于中国菜刀,如何"切菜"
介绍 经典标题党,中国菜刀有大牛已经分析过了->传送门(http://blog.csdn.net/p656456564545/article/details/49671829).博主PHP刚接触 ...
- C# - Visual Studio简明操作
Visual Studio简明操作 安装Northwind示例数据库 运行安装程序,结束安装后,再CMD中输入以下命令 cd C:\SQL Server Sample Databases(回车) s ...