left join中关于where和on条件的几个知识点:
1.多表left join是会生成一张临时表,并返回给用户
2.where条件是针对最后生成的这张临时表进行过滤,过滤掉不符合where条件的记录,是真正的不符合就过滤掉。
3.on条件是对left join的右表进行条件过滤,但依然返回左表的所有行,右表中没有的补为NULL
4.on条件中如果有对左表的限制条件,无论条件真假,依然返回左表的所有行,但是会影响右表的匹配值。也就是说on中左表的限制条件只影响右表的匹配内容,不影响返回行数。
结论:
1.where条件中对左表限制,不能放到on后面
2.where条件中对右表限制,放到on后面,会有数据行数差异,比原来行数要多 测试:
创建两张表:
CREATE TABLE t1(id INT,name VARCHAR(20));
insert into `t1`(`id`,`name`) values (1,'a11');
insert into `t1`(`id`,`name`) values (2,'a22');
insert into `t1`(`id`,`name`) values (3,'a33');
insert into `t1`(`id`,`name`) values (4,'a44'); CREATE TABLE t2(id INT,local VARCHAR(20));
insert into `t2`(`id`,`local`) values (1,'beijing');
insert into `t2`(`id`,`local`) values (2,'shanghai');
insert into `t2`(`id`,`local`) values (5,'chongqing');
insert into `t2`(`id`,`local`) values (6,'tianjin'); 测试01:返回左表所有行,右表符合on条件的原样匹配,不满足条件的补NULL
root@localhost:cuigl 11:04:25 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id;
+------+------+----------+
| id | name | local |
+------+------+----------+
| 1 | a11 | beijing |
| 2 | a22 | shanghai |
| 3 | a33 | NULL |
| 4 | a44 | NULL |
+------+------+----------+
4 rows in set (0.00 sec) 测试02:on后面增加对右表的限制条件:t2.local='beijing'
结论02:左表记录全部返回,右表筛选条件生效
root@localhost:cuigl 11:19:42 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id and t2.local='beijing';
+------+------+---------+
| id | name | local |
+------+------+---------+
| 1 | a11 | beijing |
| 2 | a22 | NULL |
| 3 | a33 | NULL |
| 4 | a44 | NULL |
+------+------+---------+
4 rows in set (0.00 sec) 测试03:只在where后面增加对右表的限制条件:t2.local='beijing'
结论03:针对右表,相同条件,在where后面是对最后的临时表进行记录筛选,行数可能会减少;在on后面是作为匹配条件进行筛选,筛选的是右表的内容。
root@localhost:cuigl 11:20:07 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t2.local='beijing';
+------+------+---------+
| id | name | local |
+------+------+---------+
| 1 | a11 | beijing |
+------+------+---------+
1 row in set (0.01 sec) 测试04:t1.name='a11' 或者 t1.name='a33'
结论04:on中对左表的限制条件,不影响返回的行数,只影响右表的匹配内容
root@localhost:cuigl 11:24:46 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id and t1.name='a11';
+------+------+---------+
| id | name | local |
+------+------+---------+
| 1 | a11 | beijing |
| 2 | a22 | NULL |
| 3 | a33 | NULL |
| 4 | a44 | NULL |
+------+------+---------+
4 rows in set (0.00 sec)
root@localhost:cuigl 11:25:04 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id and t1.name='a33';
+------+------+-------+
| id | name | local |
+------+------+-------+
| 1 | a11 | NULL |
| 2 | a22 | NULL |
| 3 | a33 | NULL |
| 4 | a44 | NULL |
+------+------+-------+
4 rows in set (0.00 sec) 测试05:where t1.name='a33' 或者 where t1.name='a22'
结论05:where条件是在最后临时表的基础上进行筛选,显示只符合最后where条件的行
root@localhost:cuigl 11:25:15 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t1.name='a33';
+------+------+-------+
| id | name | local |
+------+------+-------+
| 3 | a33 | NULL |
+------+------+-------+
1 row in set (0.00 sec)
root@localhost:cuigl 11:27:27 >SELECT t1.id,t1.name,t2.local FROM t1 LEFT JOIN t2 ON t1.id=t2.id where t1.name='a22';
+------+------+----------+
| id | name | local |
+------+------+----------+
| 2 | a22 | shanghai |
+------+------+----------+
1 row in set (0.00 sec)

  

mysql left join中where和on条件的区别的更多相关文章

  1. mysql left join中on后加条件判断和where中加条件的区别

    left join中关于where和on条件的几个知识点: .多表left join是会生成一张临时表,并返回给用户 .where条件是针对最后生成的这张临时表进行过滤,过滤掉不符合where条件的记 ...

  2. ql语句中left join和inner join中的on与where的区别分析

    sql语句中left join和inner join中的on与where的区别分析   关于SQL SERVER的表联接查询INNER JOIN .LEFT JOIN和RIGHT JOIN,经常会用到 ...

  3. sql语句中left join、inner join中的on与where的区别

    table a(id, type): id     type ---------------------------------- 1      1 2      1 3      2 table b ...

  4. join 中的on和where的区别

    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表, 然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1.on条件是在生成临时表时使用的条 ...

  5. 在join中,on和where的区别

    两个表在,join时,首先做一个笛卡尔积,on后面的条件是对这个笛卡尔积做一个过滤形成一张临时表,如果没有where就直接返回结果,如果有where就对上一步的临时表再进行过滤. 在使用left  j ...

  6. Linq→join中指定多个条件

    还是习惯先撸一段SQL * FROM User_Pic P AND P.Guid = R.UserPicGuid ORDER BY PicSize DESC 然后发现Linq中的join不能多条件.. ...

  7. linq中如何在join中指定多个条件

    public ActionResult Edit(int id) { using (DataContext db = new DataContext(ConfigurationManager.Conn ...

  8. sql语句中left join和inner join中的on与where的区别分析

    关于SQL SERVER的表联接查询INNER JOIN .LEFT JOIN和RIGHT JOIN,经常会用到ON和WHERE的条件查询,以前用的时候有时是凭感觉的,总是没有搞清楚,今日亲自测试了下 ...

  9. mysql 和 sqlserver中备份一张表的区别

    sqlserver中备份一张表 SELECT * into qa_buglist_bak FROM qa_buglist 不管表的数据有多大,速度很快: mysql中上述语句就无效了,须得新建一张表, ...

随机推荐

  1. linux下如何启动nginx?

    命令: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ........... [root@localhost sbin ...

  2. 对 js 高程 Preflighted Reqeusts 的理解

    看JS高程遇到 Preflighted Reqeusts不大理解,遂百度下: 转自:http://todoit.me/ajax-preflight/ 最近在做一个 VUE 的项目的时候, 和后端的小伙 ...

  3. HDU3535——AreYouBusy

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题目意思:给出两个数n,T,分别表示有n个任务集合,T的总时间,对于每个任务集合有两个属性m和t ...

  4. Storm-源码分析- timer (backtype.storm.timer)

    mk-timer timer是基于PriorityQueue实现的(和PriorityBlockingQueue区别, 在于没有阻塞机制, 不是线程安全的), 优先级队列是堆数据结构的典型应用 默认情 ...

  5. SOI简单体验

    导语 arcgis for server10.3.1中提供了一个新的功能叫做soi.本文简单的介绍soi概念,实现,在使用过程中的注意事项.阅读本文和使用soi需要以下先决条件 SOI是ArcGIS ...

  6. apache2.2版本 configuration error: couldn't perform authentication. AuthType not set!: /

    configuration error:  couldn't perform authentication. AuthType not set!: /  500服务器错误 解决方案: <Dire ...

  7. scrapy之中间件

    中间件的简介 1.中间件的作用 在scrapy运行的整个过程中,对scrapy框架运行的某些步骤做一些适配自己项目的动作. 例如scrapy内置的HttpErrorMiddleware,可以在http ...

  8. spring 自定义事物同步器(一): TransactionSynchronizationManager 解析

    一..JPA 获取 Hibernate的session try { session = entityManager.unwrap(Session.class); } catch (Exception ...

  9. 安装python3 centos

    1.在新centos中安装python3的步骤https://www.cnblogs.com/lclq/archive/2016/06/27/5620196.html 2.安装python3过程中报错 ...

  10. django 密文 cookie 加密

    默认cookie是明文 # 加密cookie salt 通过这个字符串把cookie内容加密 obj.set_signed_cookie('username111','aaaa',salt=" ...