前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >left join 过滤条件写在on后面和写在where 后面的区别

left join 过滤条件写在on后面和写在where 后面的区别

作者头像
java达人
发布2018-01-31 17:01:52
1.5K0
发布2018-01-31 17:01:52
举报
文章被收录于专栏:java达人java达人
代码语言:js
复制
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后t1表为

1 , 1

2 , 1

用它来left join t2.

--下面三条语句查询结果是一样的,当为右表加条件的时候,可以把left join 改为inner jin, 因为inner join比left join 要快!

代码语言:js
复制
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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-09-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 java达人 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档