设为首页 加入收藏

TOP

有意思的left join语句
2014-11-24 07:36:30 来源: 作者: 【 】 浏览:3
Tags:有意思 left join 语句

有意思的left join语句
问题的初衷是,在一个SQL语句中,有left join, 我加条件在where后面发生死锁,于是想是否可以把条件加入到left join的on后面? www.2cto.com
也就是下面两个语句是否效果一样:
语句1: select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1
语句2: select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1
数据环境:
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);
经过验证发现: 语句1可以这样来理解,把left join 的on后面的所有条件看成一个整体,这里就是t1.id=t2.id and t1.feild=1看成一个整体,坐连接的时候,串t2表,看是否有满足条件的记录,如果没有就串null值。
语句2的理解简单了,现在left join,在最后结果集中做where过滤
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Skew Join与Left Semi Join相关 下一篇Mongodb操作详解(二)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis压力测试实战 - (2025-12-27 09:20:24)
·高并发一上来,微服 (2025-12-27 09:20:21)
·Redis 高可用架构深 (2025-12-27 09:20:18)
·Linux 系统监控 的完 (2025-12-27 08:52:29)
·一口气总结,25 个 L (2025-12-27 08:52:27)