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
left join 过滤条件写在on后面和写在where 后面的区别的更多相关文章
- SQL语句中过滤条件放在on、where、having的区别和联系
摘要:SQL语句中,过滤条件放在不同筛选器on.where和having的区别和联系. 综述 在<SQL语句中过滤条件放在on和where子句中的区别和联系>中,介绍了多表关联SQL语 ...
- SQL优化 查询语句中,用 inner join 作为过滤条件和用where作为过滤条件的区别
前段时间遇到一个存储过程,参数之一是一个字符串,在存储过程中,把字符串拆分成一个临时表之后存为一个key值的临时表,作为其中一个查询条件, 逻辑实现上有两种处理方式 insert into #t se ...
- SQL server 使用 内联结(INNER JOIN) 联结多个表 (以及过滤条件 WHERE, AND使用区别)
INNER JOIN ……ON的语法格式: FROM (((表1 INNER JOIN 表2 ON 表1.字段号=表2.字段号) INNER JOIN 表3 ON 表1.字段号=表3.字段号) INN ...
- .NET深入实战系列--EF到底怎么写过滤条件
本文唯一访问地址:http://www.cnblogs.com/yubaolee/p/DynamicLinq.html 对于系统开发来说,按不同字段进行过滤查询是一种常见的需求.在EF中通常的做法是: ...
- .NET深入实战系列--EF到底怎么写过滤条件(转)
原文来自:http://www.cnblogs.com/yubaolee/p/DynamicLinq.html 对于系统开发来说,按不同字段进行过滤查询是一种常见的需求.在EF中通常的做法是: /// ...
- hive join on 条件 与 where 条件区别
1. select * from a left join b on a.id = b.id and a.dt=20181115; 2. select * from a left join b on a ...
- SQL语句中过滤条件放在on和where子句中的区别和联系
摘要: 介绍在多表关联SQL语句中,过滤条件放在on和where子句中的区别--inner join中没区别,外连接就不一样. 综述 蚂蚁金服的一道SQL面试题如下:SQL语句中,过滤条件放在on ...
- 自定义 Azure Table storage 查询过滤条件
本文是在Azure Table storage 基本用法一文的基础上,介绍如何自定义 Azure Table storage 的查询过滤条件.如果您还不太清楚 Azure Table storage ...
- magento addFieldToFilter()方法常用的过滤条件
记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件.以下参数也同样适用于产品实体的addAttributeToFilter()方法. ...
随机推荐
- HttpContext 讲解
HttpContext类:封装有关个别HTTP请求的所有HTTP特定的信息,又叫上下文.看到这个解释,我觉得有些抽象,Http特定信息具体又是什么?看了下备注:为继承 IHttpModule 和 IH ...
- SQLite&&SharedPreferences&&IO读写Sdcard学习笔记
SQLite 轻量级的.嵌入式的.关系型数据库 Android.IOS等广泛使用的的数据库系统 SQLite数据库之中可以方便的使用SQL语句,实现数据的增加.修改.删除.查询等操作 SQLiteOp ...
- .net上传图片并转成二进制流
话不多说,直接上代码 <input id="InputFile" style="width: 399px" type="file" r ...
- 字符集转换: Ansi - Unicode
字符集转换: Ansi - Unicode wstring AnsiToUnicode (const string& strSrc ) { /*!< 分配目标空间 */ ,strSrc. ...
- C++学习——类的继承
公有继承(public).私有继承(private).保护继承(protected)是常用的三种继承方式. 1. 公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时, ...
- C# 枚举操作扩展类
using System; using System.Linq; using System.ComponentModel; namespace Demo.Common { /// <summar ...
- 【风马一族_Python】 安装pip与Numpy
------------------------------------------------------------------------------------------------- 原因 ...
- Linux操作杂记
centos7修改默认运行等级 查看当前默认运行等级: systemctl get-dafault 修改默认运行等级为5: systemctl set-default graphical.target ...
- Linux 我的第一个makefile(Linux指令学习笔记)
我的第一个makefile 最近学到了makefile的文件的编写.makefile是一个能达到方便编译链接生成目标程序的文件, make确实很方便,在写makefile的过程也能更好的理解gcc编译 ...
- stanford moss
A System for Detecting Software Plagiarism UPDATES May 18, 2014 Community contributions (incuding a ...