首先是针对左右连接,这里与inner join区分

在使用left join时,on and 和on where会有区别

1. on的条件是在连接生成临时表时使用的条件,以左表为基准 ,不管on中的条件真否,都会返回左表中的记录
2.where条件是在临时表生成好后,再对临时表过滤。此时 和left join有区别(返回左表全部记录),条件不为真就全部过滤掉,on后的条件来生成左右表关联的临时表,
where后的条件是生成临时表后对临时表过滤

on and是进行韦恩运算时 连接时就做的动作,where是全部连接完后,再根据条件过滤

CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sid` int(11) NOT NULL,
`type` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `a` (`id`, `sid`, `type`) VALUES (1, 1, 'a');
INSERT INTO `a` (`id`, `sid`, `type`) VALUES (2, 1, 'b');
INSERT INTO `a` (`id`, `sid`, `type`) VALUES (3, 2, 'c');
INSERT INTO `a` (`id`, `sid`, `type`) VALUES (4, 3, 'd'); CREATE TABLE `b` (
`sid` int(11) NOT NULL,
`remark` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `b` (`sid`, `remark`) VALUES (1, 'A');
INSERT INTO `b` (`sid`, `remark`) VALUES (2, 'B');
INSERT INTO `b` (`sid`, `remark`) VALUES (3, 'C');
INSERT INTO `b` (`sid`, `remark`) VALUES (4, 'D');
mysql> select * from a;
+----+-----+------+
| id | sid | type |
+----+-----+------+
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 2 | c |
| 4 | 3 | d |
+----+-----+------+
4 rows in set mysql> select * from b;
+-----+--------+
| sid | remark |
+-----+--------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
+-----+--------+
4 rows in set
mysql> select * from a left join b on a.sid=b.sid;
+----+-----+------+-----+--------+
| id | sid | type | sid | remark |
+----+-----+------+-----+--------+
| 1 | 1 | a | 1 | A |
| 2 | 1 | b | 1 | A |
| 3 | 2 | c | 2 | B |
| 4 | 3 | d | 3 | C |
+----+-----+------+-----+--------+
mysql> select * from a left join b on a.sid=b.sid and a.sid=1;
+----+-----+------+------+--------+
| id | sid | type | sid | remark |
+----+-----+------+------+--------+
| 1 | 1 | a | 1 | A |
| 2 | 1 | b | 1 | A |
| 3 | 2 | c | NULL | NULL |
| 4 | 3 | d | NULL | NULL |
+----+-----+------+------+--------+
mysql> select * from a left join b on a.sid=b.sid where a.sid=1;
+----+-----+------+-----+--------+
| id | sid | type | sid | remark |
+----+-----+------+-----+--------+
| 1 | 1 | a | 1 | A |
| 2 | 1 | b | 1 | A |
+----+-----+------+-----+--------+

对于inner join

mysql> select * from a inner join b on a.sid=b.sid and a.sid=1;
+----+-----+------+-----+--------+
| id | sid | type | sid | remark |
+----+-----+------+-----+--------+
| 1 | 1 | a | 1 | A |
| 2 | 1 | b | 1 | A |
+----+-----+------+-----+--------+
mysql> select * from a inner join b on a.sid=b.sid where a.sid=1;
+----+-----+------+-----+--------+
| id | sid | type | sid | remark |
+----+-----+------+-----+--------+
| 1 | 1 | a | 1 | A |
| 2 | 1 | b | 1 | A |
+----+-----+------+-----+--------+

on and和on where结果一致
在使用inner join时,不管是对左表还是右表进行筛选,on and和on where都会对生成的临时表进行过滤

MYSQL 表左连接 ON AND 和ON WHERE 的区别的更多相关文章

  1. 深入浅出:MySQL的左连接、右连接、等值连接

    深入浅出:MySQL的左连接.右连接.等值连接 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表对象进行的连接操作原理也是一样的. 1.左连接(LEF ...

  2. MySQL的左连接、右连接和全连接的实现

    表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...

  3. MySQL之左连接与右连接

    左连接: select 列1,列2,列N from tableA left join tableB on tableA.列 = tableB.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...

  4. Mysql之左连接右连接内连接——示例 (转)

    下面是两张表 表stu 表tech 1.右连接 当使用右连接语句查询时,返回结果如下: 1 SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.na ...

  5. MySQL 使用左连接替换not in

    众所周知,左连接和右连接的含义是以哪一张表为准. 左连接就是以左表为准,查出的结果中包含左表所有的记录,如果右表中没有与其对应的记录,那么那一行记录中B表部分的内容就全是NULL. 现在有两个表,一个 ...

  6. 【数据库】MySQL的左连接、右连接和全连接的实现

    表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...

  7. 深入浅出:MySQL的左连接、右连接、内连接

    http://blog.csdn.net/wyzxg/article/details/7276979 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表 ...

  8. mysql之左连接、右连接、内连接、全连接、等值连接、交叉连接等

    mysql中的各种jion的记录,以备用时查 1.等值连接和内连接, a.内连接与等值连接效果是相同的,执行效率也相同,只是书写方式不一样,内连接是由SQL 1999规则定的书写方式 比如: sele ...

  9. MySql之左连接,右连接

    左连接,右连接查询的表 中 on后面的条件不会影响主表的数据,只会影响右表的数据. 例: 没加条件的时候 左表加条件: 右表加条件: 通过上面3处对比可以看出来,用LEFT JOIN 的时候不管对左表 ...

随机推荐

  1. 19届华为实习生笔试之判断iPv6地址类型

    题二: 答案: #coding=utf-8 import re,sys str = sys.stdin.readline().strip() def regex(str): result = &quo ...

  2. Javascript 判断传入的两个数组是否相似

    任务描述: 请在index.html文件中,编写arraysSimilar函数,实现判断传入的两个数组是否相似.具体需求: 1. 数组中的成员类型相同,顺序可以不同.例如[1, true] 与 [fa ...

  3. Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务

    hadoop api提供了一些遍历文件的api,通过该api可以实现遍历文件目录: import java.io.FileNotFoundException; import java.io.IOExc ...

  4. Django(博客系统):重写了auth.User后使用createsupperuser出错解决办法

    背景:重写django的系统User后,使用createsupperuser创建用户失败 由于项目需要扩展django默认新的auth.User系统(添加两个字段:头像.简介等字段),因此就重写了dj ...

  5. hdu 6095 Rikka with Competition---思维题贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6095 题目大意: 任意两个人相比,相差大于K,分低的淘汰,否则两人都有可能赢,剩下的继续比,问有最多 ...

  6. 前端开发必备之chrome插件

    Chrome浏览器目前是网络上可用的最好的浏览器之一,并且自2011年11月超越了Firefox浏览器之后,已经成为了互联网上占主导地位的浏览器. 本篇文章将与大家分享一些与前端开发有关的实用的Chr ...

  7. Genymotion下载慢或者下载失败的解决办法

    转.原文地址:http://blog.csdn.net/sean_css/article/details/52674091 办法如下: 1.首先点击界面上的 + 号(Add)按钮,选择你要下载的模拟器 ...

  8. 文件上传详解 (HTML FILE)

    FileUpload 对象 在 HTML 文档中 <input type="file"> 标签每出现一次,一个 FileUpload 对象就会被创建. 该元素包含一个文 ...

  9. [LeetCode] Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  10. [LeetCode] Exclusive Time of Functions 函数的独家时间

    Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...