left join 过滤条件写在on后面和写在where 后面的区别
2012-12-25 13:05
549 查看
create table t1(id int, feild int);
insert into t1 values(1 , 1);
insert into t1 values(1 , 2);
insert into t1 values(1 , 3);
insert into t1 values(1 , 4);
insert into t1 values(2 , 1);
insert into t1 values(2 , 2);
create table t2(id int, feild int);
insert into t2 values(1 , 1);
insert into t2 values(1 , 2);
insert into t2 values(1 , 5);
insert into t2 values(1 , 6);
insert into t2 values(2 , 1);
insert into t2 values(2 , 3);
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id --取t1表的第一行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
然后取t1表的第二行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
重复以上过程,直到t1表扫描结束.
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1 --给左表加条件的时候,左表满足条件的,按上面的过程返回值,左表不满足条件的,直接输出,右表的列补null
1 1 1 1
1 1 1 2
1 1 1 5
1 1 1 6
2 1 2 1
2 1 2 3
1 2 NULL NULL
1 3 NULL NULL
1 4 NULL NULL
2 2 NULL NULL
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1 先执行where后连接查询
执行where后表为 1 , 1
2 , 1
用它来left join t2.
--下面三条语句查询结果是一样的,当为右表加条件的时候,可以把left join 改为inner jin, 因为inner join比left join 要快!
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t2.feild=1
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t2.feild=1
select t1.*,t2.* from t1 inner join t2 on t1.id=t2.id and t2.feild=1
注:当添加的过滤条件为右边表的过滤条件,则左边表的即便LEFT JOIN 也不会保留所有左边表的记录数据,而是根据过滤条件过滤后的记录数。故left join 和 inner join 效果一样。
insert into t1 values(1 , 1);
insert into t1 values(1 , 2);
insert into t1 values(1 , 3);
insert into t1 values(1 , 4);
insert into t1 values(2 , 1);
insert into t1 values(2 , 2);
create table t2(id int, feild int);
insert into t2 values(1 , 1);
insert into t2 values(1 , 2);
insert into t2 values(1 , 5);
insert into t2 values(1 , 6);
insert into t2 values(2 , 1);
insert into t2 values(2 , 3);
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id --取t1表的第一行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
然后取t1表的第二行,扫瞄t2表,按条件做对比,如果满足条件,就加入返回结果表.
重复以上过程,直到t1表扫描结束.
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1 --给左表加条件的时候,左表满足条件的,按上面的过程返回值,左表不满足条件的,直接输出,右表的列补null
1 1 1 1
1 1 1 2
1 1 1 5
1 1 1 6
2 1 2 1
2 1 2 3
1 2 NULL NULL
1 3 NULL NULL
1 4 NULL NULL
2 2 NULL NULL
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1 先执行where后连接查询
执行where后表为 1 , 1
2 , 1
用它来left join t2.
--下面三条语句查询结果是一样的,当为右表加条件的时候,可以把left join 改为inner jin, 因为inner join比left join 要快!
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t2.feild=1
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t2.feild=1
select t1.*,t2.* from t1 inner join t2 on t1.id=t2.id and t2.feild=1
注:当添加的过滤条件为右边表的过滤条件,则左边表的即便LEFT JOIN 也不会保留所有左边表的记录数据,而是根据过滤条件过滤后的记录数。故left join 和 inner join 效果一样。
相关文章推荐
- left join 过滤条件写在on后面和写在where 后面的区别
- left join 过滤条件写在on后面和写在where 后面的区别
- left join 过滤条件写在on后面和写在where 后面的区别
- LEFT JOIN(联表)的条件写在ON中和WHERE的区别
- left join条件写在on中和where中的区别
- T-SQL过滤语句写在on和where上的区别
- left join加上on条件与where条件区别
- left join on、where后面的条件的区别
- LEFT JOIN ON 后面跟多个条件和Where的区别
- sql 左连接(left join),右链接(right join) 条件放在on和放在where 的区别
- T-SQL过滤语句写在on和where上的区别
- JOIN联表中ON,WHERE后面跟条件的区别
- sql 条件加在 left join 与where后面的区别
- mysql中left join中的on条件 和 where条件区别
- JOIN联表中ON,WHERE后面跟条件的区别
- JOIN关联表中ON,WHERE后面跟条件的区别
- left join(on和where条件放置的区别)
- MYSQL联表查询LEFT JOIN 中 WHERE条件放在ON条件里和外部的区别
- oracle数据库sql语句left join,right join,inner join的条件on和where的区别
- join联表中on,where后面跟条件的区别