在使用left jion时,on和where条件的区别如下:
1、 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
2、where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
表1:tab2
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’)
SQL1 :
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 |
SQL2 :
中间表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_on_and与left_join_on_where的区别
本篇文章是对Oracle的left join中on和where的区别进行了详细的分析介绍,需要的朋友参考下
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 关键字会从左表 (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 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。 如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有列为 NULL 的数据 在匹配阶段 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 ...
主要介绍了mysql中left join设置条件在on与where时的用法区别,结合实例形式分析了mysql中left join设置条件在on与where时的相关用法区别与操作注意事项,需要的朋友可以参考下
本篇文章是对mysql left( right )join使用on与where筛选的差异进行了详细的分析介绍,需要的朋友参考下
table a(id, type):id type ———————————-1 1 2 1 3 2 table b(id, class):id class ———————————1 ...sql语句2:select a.*, b.* from a left join b on a.id = b.id where a.type = 1;s
我们在写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
解决办法呢,就是将条件放到 LEFT JOIN 中。 MySQL 语句执行顺序 首先先说明一个概念,MySQL 语句执行的顺序,并不是按照 SQL 语句的顺序。下面是示例 SQL SELECT DISTINCT < select_list > FROM < left> ...
代码如下: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...