left join on and与left join on where的区别
来源:互联网 发布:宁远县优化办 编辑:程序博客网 时间:2024/06/05 09:11
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
在使用left jion时,on和where条件的区别如下:
1、on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
假设有两张表:
create table tab1(id int, size int);
insert into tab1 values(1,10),(2,20),(3,30);
***************
表1 tab1:
id size
1 10
2 20
3 30
create table tab2(size int, name varchar(10));
insert into tab2 values(10,'AAA'),(20,'BBB'),(20,'CCC');
注意,第二个表没有30
**************
表2 tab2:
size name
10 AAA
20 BBB
20 CCC
**************
两条SQL:
1、select * from tab1 left join tab2 on (tab1.size = tab2.size) where tab2.name=’AAA’
2、select * from tab1 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)
心得:
1. left join其实也会计算笛卡尔乘积(使用On给的条件来计算),所不同的是部分数据没有笛卡尔乘积所对应的时候,照样会把左表的数据列出来,右表数据用NULL来填充。
2. 然后使用Where语句过滤。(就是先join计算笛卡尔乘积+补全显示左表,后where过滤前者的结果)
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)
心得:笛卡尔乘积的条件过于苛刻(的时候),只有一条数据符合要求。但是不管这些,剩余左表数据也要显示,其值使用NULL来填充。
后来的心得:就是先过滤右表的数据,然后再Left Join,得出的结果当然是左表的三条数据。
其实以上结果的关键原因就是left join,right join,full join的特殊性,不管on上的条件是否为真都会返回left或right表中的记录,full则具有left和right的特性的并集。 而inner jion没这个特殊性,则条件放在on中和where中,返回的结果集是相同的。
http://sibyl-pisces.iteye.com/blog/1246411
实际应用,而我的这句语句:
$sql ="select * FROM u_role LEFT JOIN v_job_type ON u_role.role_type=v_job_type.job_type_id ";
$sql.="ORDER BY role_type ";
不会出现重复的角色是因为,它的外键role_type所对应的数据在v_job_type是主键,绝对不会重复。(本来left join理论上要计算笛卡尔乘积,也会出现重复值的)
所以这里每个角色也不会重复出现。
- left join on ...and ... 与left join on ... where.. 的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and 与 left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- left join on and与left join on where的区别
- stm32l01系列单片机通过串口编程(ISP)
- Matlab笔记:对数组的基本操作
- Java中基本数据类型和包装器类型的关系
- Android仿ios加载时菊花图
- linq指定列查询、去重复、排序、top行
- left join on and与left join on where的区别
- Android中应用调用系统权限
- 标签meta link 表格
- 使用sfl4j和logback在spring boot中进行日志配置
- Maven日常 —— 你应该知道的一二三
- Android接入多盟广告SDK--让你的APP通过广告平台赚钱
- linux配置java环境变量(详细)
- 远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
- ajax返回json字符串转换为JSON对象