数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。
在使用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 * form tab1 left join tab2 on (tab1.size = tab2.size) where tab2.name=’AAA’
2、select * form 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中,返回的结果集是相同的。
http://kiss2375.iteye.com/blog/1036533
分享到:
相关推荐
left_join_on_and与left_join_on_where的区别
Join on/inner join on/full join on/full outer join on/left join on/right join on/cross join on; 在使用jion时,on和where条件的区别;
left join 过滤条件写在on后面和写在where 后面的区别
本篇文章是对Oracle的left join中on和where的区别进行了详细的分析介绍,需要的朋友参考下
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。 LEFT JOIN 关键字语法 SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1....
优先级 ...在left join下,两者的区别: on是在生成临时表的时候使用的条件,不管on的条件是否起到作用,都会返回左表 (table_name1) 的行。 where则是在生成临时表之后使用的条件,此时已经不管是否使
ON 子句与 WHERE 子句的不同 一种更好地理解带有 WHERE … IS NULL 子句的复杂匹配条件的简单方法 Matching-Conditions 与 Where-conditions 的不同 关于 “A LEFT JOIN B ON 条件表达式” 的一点提醒 ON 条件(...
主要介绍了mysql中left join设置条件在on与where时的用法区别,结合实例形式分析了mysql中left join设置条件在on与where时的相关用法区别与操作注意事项,需要的朋友可以参考下
我们在写SQL时候,往往会需要格式化或多表联合查询,现在来一个实例看看这个问题 SELECT t.id, t.topic_...LEFT JOIN student_commit_history sch on t.id = sch.topic_id where sch.student_id =41 AND sch.create_t
本篇文章是对mysql left( right )join使用on与where筛选的差异进行了详细的分析介绍,需要的朋友参考下
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
table a(id, type):id type ———————————-1 1 2 1 3 2 table b(id, class):id class ———————————1 12 2sql语句1:select a.*, b.* from a left join b on a.id = b.id and a.type = ...
代码如下:select a.f_username from ( SELECT /*+parallel(gu,4)*/distinct gu.f_username FROM t_base_...playid=4 and gu.f_paymoney>=1500 ) A left join ( select from t_base_vip_customes and ((vu.f_passeddate
[t0].[Discontinued] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Suppliers] AS [t1] ON [t1].[SupplierID] = [t0].[SupplierID] WHERE ([t1].[Country] = @p0) AND ([t0].[UnitsInStock] = @p1) -- @p0...
update a set a.up_end=up*@zk/100,a.dis_cnt=@zk,a.amtn=up*@zk/100*qty,a.amt=up*@zk/100*qty, a.tax=0 from tf_pos a left join tf_pos_z b on a.os_no=b.os_no where a.os_no=@os_no and a.itm=@itm ...