首先是针对左右连接,这里与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. 新概念英语(1-127)A famous actoress(女演员)

    A:Can you recognize that woman, Liz ?B:I think I can, Kate. It must be Karen Marsh, the actoress.A:I ...

  2. 阿里云API网关(11)API的三种安全认证方式

    网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...

  3. 在Android项目中使用Java8

    前言 在过去的文章中我介绍过Java8的一些新特性,包括: Java8新特性第1章(Lambda表达式) Java8新特性第2章(接口默认方法) Java8新特性第3章(Stream API) 之前由 ...

  4. python/匿名函数和内置函数

    1 匿名函数 匿名函数是lambda定义的没有名字的具有一些小功能的函数 具体形式是 lambda 参数列表:返回值表达式 lambda x: X**2 # 求平方操作 lambda x: x> ...

  5. Struts(二十一):类型转换与复杂属性、集合属性配合使用

    背景: 本章节主要以复杂属性.集合属性类型转化为例,来学习这两种情况下怎么使用. 复杂对象属性转换场景: 1.新建struts_04 web.xml <?xml version="1. ...

  6. Bellman-Ford算法的改进---SPFA算法

    传送门: Dijkstra Bellman-Ford SPFA Floyd 1.算法思想 Bellman-Ford算法时间复杂度比较高,在于Bellman-Ford需要递推n次,每次递推需要扫描所有的 ...

  7. 华为防火墙USG5500-企业双ISP出口

    需求:(1)技术部IP地址自动获取,网段为192.168.10.0/24,该部门访问Internet的报文正常情况下流入链路ISP1. 总经办IP地址自动获取,网段为192.168.20.0/24,该 ...

  8. WPF Uri

    场景:自定义控件Generic.xaml样式引用资源字典Dictionary1.xaml. 方式:绝对路径. 方式1: <ResourceDictionary> <ResourceD ...

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

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

  10. python学习记录 - python3.x中如何实现print不换行

    python3.x中如何实现print不换行   大家应该知道python中print之后是默认换行的, 那如何我们不想换行,且不想讲输出内容用一个print函数输出时,就需要改变print默认换行的 ...