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
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 要快!
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
最新文章
- unity 发布web player版,网页打开报Failed to initialize player's 3D settings
- LIKE模糊查询的通配符
- Oracle死锁查询及处理
- mongodb在WEB开发中的应用与实践
- ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
- Python中带参装饰器理解
- ruby -- 问题解决(一)无法连接mysql数据库
- ruby 资料整理
- Linux - 扩展
- Jersey Politics
- SVProgressHUD的使用
- 单例模式(Singleton)看了就懂
- mysql5.7-Group Replication
- iOS7 CookBook精彩瞬间(一)property、selector细节、__unused
- Linux 文本去重 之 命令sort 与 uniq
- [转]Understanding OpenStack Authentication: Keystone PKI
- 不使用接口的 limit 控制分页的容量
- 安装pytorch成功但cuda不可用
- python利用opencv合成模糊图像
- json.dumps与json.loads实例
热门文章
- TCP/IP TIME_WAIT状态
- 转:自建CDN防御DDoS(1, 2, 3)infoq
- 转: 微博的多机房部署的实践(from infoq)
- winform分页管理
- [CAMCOCO][C#]我的系统架构.服务器端.(二)----DATA层
- java创建Date日期时间笔记
- [老老实实学WCF] 第九篇 消息通信模式(上) 请求应答与单向
- 实例介绍Cocos2d-x开关菜单
- ARM你必须知道的事儿——为啥“PC = PC + 8”?
- hdu 六度分离 floyd