left join条件写在on中和where中的区别

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


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.


 

 --下面三条语句查询结果是一样的

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值