left join后面加上where条件浅析

来源:互联网 发布:ip绑定mac地址 编辑:程序博客网 时间:2024/05/22 15:36

http://www.cnblogs.com/huahua035/p/5718469.html


select a.*,b.*from table1 aleft join table2 b on b.X=a.Xwhere XXX

如上:一旦使用了left join,没有where条件时,左表table1会显示全部内容

    使用了where,只有满足where条件的记录才会显示(左表显示部分或者全部不显示)

so。。。。

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

 

原因分析:

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

where条件是在临时表生成好后,再对临时表进行过滤的条件;

因此:where 条件加上,已经没有left join的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。 

 

解决方案:

1、where过滤结果作为子查询,和主表left,如下:

复制代码
select a.*,tmp.*from table1 aleft join(    select a.*,b.*    from table1 a    left join table2 b on b.X=a.X    where XXX)tmp
复制代码

很明显,子查询语句无论 left join、inner join都没啥区别了

2、查询条件放在on后面

1
2
3
select a.*,b.*
from table1 a
left join table2 b on b.X=a.X and XXX

注意:where XXX去掉,改为链接条件on后面的 and XXX

分析:

 on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。

 

结论:


 

过滤条件放在:

where后面:是先连接然生成临时查询结果,然后再筛选

on后面:先根据条件过滤筛选,再连 生成临时查询结果

 


0 0