MYSQL 表左连接 ON AND 和ON WHERE 的区别
首先是针对左右连接,这里与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 的区别的更多相关文章
- 深入浅出:MySQL的左连接、右连接、等值连接
深入浅出:MySQL的左连接.右连接.等值连接 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表对象进行的连接操作原理也是一样的. 1.左连接(LEF ...
- MySQL的左连接、右连接和全连接的实现
表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...
- MySQL之左连接与右连接
左连接: select 列1,列2,列N from tableA left join tableB on tableA.列 = tableB.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...
- Mysql之左连接右连接内连接——示例 (转)
下面是两张表 表stu 表tech 1.右连接 当使用右连接语句查询时,返回结果如下: 1 SELECT stu.id,stu.name,stu.classe_name,tech.id,tech.na ...
- MySQL 使用左连接替换not in
众所周知,左连接和右连接的含义是以哪一张表为准. 左连接就是以左表为准,查出的结果中包含左表所有的记录,如果右表中没有与其对应的记录,那么那一行记录中B表部分的内容就全是NULL. 现在有两个表,一个 ...
- 【数据库】MySQL的左连接、右连接和全连接的实现
表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...
- 深入浅出:MySQL的左连接、右连接、内连接
http://blog.csdn.net/wyzxg/article/details/7276979 三种连接的语法 为便于更多的技友快速读懂.理解,我们只讨论2张表对象进行连接操作的情况,大于2张表 ...
- mysql之左连接、右连接、内连接、全连接、等值连接、交叉连接等
mysql中的各种jion的记录,以备用时查 1.等值连接和内连接, a.内连接与等值连接效果是相同的,执行效率也相同,只是书写方式不一样,内连接是由SQL 1999规则定的书写方式 比如: sele ...
- MySql之左连接,右连接
左连接,右连接查询的表 中 on后面的条件不会影响主表的数据,只会影响右表的数据. 例: 没加条件的时候 左表加条件: 右表加条件: 通过上面3处对比可以看出来,用LEFT JOIN 的时候不管对左表 ...
随机推荐
- MYSQL之索引原理与慢查询优化
一.索引 1.介绍 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的也是最容易出现问题的,还是一些复杂的查询操作,因此对查询语句的优化 ...
- hdu1222&hdu1014 循环群的生成元
hdu1222 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1222 题目大意: 大灰狼追小白兔.小白兔可以躲起来的洞绕成一个圈,大灰狼从0这个点出 ...
- python如何转换word格式、读取word内容、转成html
# python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...
- 爬取IP
import urllib.request import re def url_open(url): req = urllib.request.Request(url,headers={'User-A ...
- javax.el.ELException: Error reading [name] on type [com.news.entity.Topic_$$_javassist_1]异常
异常如下: 异常分析:从message中可以看出,错误是读取异常,属性是name,路径是com.news.entity.Topic,此错误是使用Hibernate时,由于Hibernate还没有去数据 ...
- 在MySQL中使用子查询
子查询作为数据源 子查询生成的结果集包含行.列数据,因而非常适合将它与表一起包含在from子句的子查询里.例: SELECT d.dept_id, d.name, e_cnt.how_many num ...
- 从让 HTTPS 更安全出发,聊聊 HTTPS
随着公众对网络安全的日益关注,各种网络安全防护手段层出不穷.HTTPS Everywhere作为提升HTTPS安全性的有效手段,日前安全性与实用性再次得到了加强. HTTPS虽然可以有效提升用户浏览网 ...
- 阿里云、腾讯云开通端口 telnet不通的原因
1.安全组是否已经开通相对应的端口: 阿里云:https://help.aliyun.com/document_detail/25471.html 腾讯云:http://bbs.qcloud.com/ ...
- CodeForces 916E Jamie and Tree(树链剖分+LCA)
To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...
- contenteditable 插入及粘贴纯文本内容
本文主要介绍 div 标签设置 contenteditable = ' true ' 时,在光标位置插入输入的内容,或在光标位置粘贴纯文本内容.文中涉及知识,可参考以下: http://www.zh ...