left join on and 与 left join on where 的区别
2017-09-22 15:46
447 查看
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
在使用left jion时,and 和 where 条件的区别如下:
1、 and 条件是在生成临时表时使用的条件,它不管 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 * 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)
(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中,返回的结果集是相同的。
-----------------------------------------------------------------------------------------------------------------------------------------------
番外篇:
在使用left jion时,and 和 where 条件的区别如下:
1、 and 条件是在生成临时表时使用的条件,它不管 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 * 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)
(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中,返回的结果集是相同的。
-----------------------------------------------------------------------------------------------------------------------------------------------
番外篇:
select * from a,b where a.id=b.id ---等值连接 select * from a inner join b on a.id = b.id -----内连接 内连接与等值连接效果是相同的,执行效率也是一样的。 只不过内连接是由SQL 1999规则定的书写方式,其实这两个是一样的。
相关文章推荐
- 关于数据库中使用 left join on ...and ...和 left join on ....where ...区别,和使用group by 要注意的情况
- 【mysql】left join on and 和 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的区别
- MySQL多表联合查询之--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的区别