Left join加上where条件的困惑

eft join加上where条件的困惑

转:百度空间

left join的困惑:一旦加上where条件,则显示的结果等于inner join

将where 换成 and

用where 是先连接然后再筛选   
用and 是先筛选再连接

数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户。

在使用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中,返回的结果集是相同的。

时间: 2024-08-08 22:05:43

Left join加上where条件的困惑的相关文章

left join on +多条件与where区别

left join on +多条件与where区别 重点 先匹配,再筛选where条件. 本文将通过几个例子说明两者的差别. 1. 单个条件 select * from product a left join on product_details b on a.id = b.id 2. 条件写在on 与where区别 查询1: SELECT * FROM product LEFT JOIN product_details ON (product.id = product_details.id)

SQL优化 查询语句中,用 inner join 作为过滤条件和用where作为过滤条件的区别

前段时间遇到一个存储过程,参数之一是一个字符串,实际作用是在存超过中是作为一个查询条件处理的 在存储过程中,把字符串拆分成一个临时表之后作key值,作为一个查询条件,逻辑实现上有两种处理方式 insert into #t select key from split_function('传递进来的字符串',',') 第一种是与物理表做inner join,类似如下 select * from tableA a inner join tableB b on a.id=b.id inner join

性能优化实战-join与where条件执行顺序

昨天经历了一场非常痛苦的性能调优过程,但是收获也是刻骨铭心的,感觉对sql引擎的原理有了进一步认识. 问题起源于测试人员测一个多条件检索的性能时,发现按某个条件查询会特别慢.对应的sql语句简化为: [sql] view plain copy print? select * from ta a,tb b where a.bid=b.id and a.col1='xx' and b.col2='yy' and dbo.func(a.col3,'zz')=1 ta表几万行,tb几百行. 大家看到了,

MySql中三种Join以及Where条件的疑惑

MySQL JOIN时条件放在Where之后和On之后结果是迥然不同的 在Left Join的时候on后的左表条件不生效 Right Join的时候on后的右表条件不生效 即左或右连接的时候on后的主表条件不生效 select * from a left join b on a.id=b.id and a.name=1 and b.age=2; 这里只有b.age=2能生效a.name=1不生效 在Inner Join的时候on后的条件不论主表从表都能生效. 在left或者right join的

Linq join on 多条件

var a = from m in DbContext.Set<T1>() join q in DbContext.Set<T2>() on new { m.ID, Phone=m.Phone1 } equals new { q.ID, Phone=q.Phone2 } where m.Phone1 !=null select new { m.ID, m.Phone1 }; a = a.OrderBy(m => m.Phone1).Skip(2).Take(2); SELEC

jpa/hibernate @onetomany 使用left join 添加多条件,可以使用过滤器filters (with-clause not allowed on fetched associations; use filters异常信息)

package com.ipinyou.mip.dataAsset.campaignManagement.entity; import com.ipinyou.mip.utils.NumberUtils; import com.ipinyou.mip.utils.StringHelper; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; import org.apache.commons.l

oracle exp导出加上过滤条件

exp username/[email protected]   file='d:\hehe.dmp' tables=(%) query="""where UPDATE_DTIME >= to_date('2017-01-01','yyyy-mm-dd')""" create user DB_USER_NAME identified by DB_USER_PWDdefault tablespace ETL; grant connect,re

Esper学习之十一:EPL语法(七)

上一篇说到了EPL如何访问关系型数据库这种数据源,实际上别的数据源,比如:webservice.分布式缓存.非关系型数据库等等,Esper提供了统一的数据访问接口.然后今天会讲解如何创建另外一种事件类型——Schema. 1.Joining Method Invocation Results和执行sql的语法类似,调用方法的一种触发方式也是通过join别的事件的属性来达到效果,且调用方法的句子为from子句.语法如下: [plain] view plaincopy method:class_na

数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别

join on 与 where 条件的执行先后顺序: join on 条件先执行,where条件后执行:join on的条件在连接表时过滤,而where则是在生成中间表后对临时表过滤 left join.right join.full join.inner join区别: left join:以左表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对左表无效 right join:以右表为基准,根据on条件过滤连接生成临时表,on后面的过滤条件对右表无效 full join:以左表为基准