创建两张表并插入一些数据

create table class(
class_id int,
class_name varchar(20),
class_grade char(1)
); insert into class values (1,'语文','A');
insert into class values (2,'数学','B');
insert into class values (3,'英语','C'); create table score(
class_id int,
stu_id varchar(20),
Score int
); insert into score values (1,'A001',91);
insert into score values (2,'A001',95);
insert into score values (1,'A002',82);
insert into score values (2,'A002',87);
insert into score values (3,'B003',65);

查看表中的数据

mysql> select * from class;
+----------+------------+-------------+
| class_id | class_name | class_grade |
+----------+------------+-------------+
| 1 | 语文 | A |
| 2 | 数学 | B |
| 3 | 英语 | C |
+----------+------------+-------------+
3 rows in set (0.00 sec) mysql> select * from score;
+----------+--------+-------+
| class_id | stu_id | Score |
+----------+--------+-------+
| 1 | A001 | 91 |
| 2 | A001 | 95 |
| 1 | A002 | 82 |
| 2 | A002 | 87 |
| 3 | B003 | 65 |
+----------+--------+-------+
5 rows in set (0.00 sec) mysql>

比较下面几组查询结果
--如果你对left join足够熟悉的话,先不要看结果,是否可以直接说出下面查询的结果

mysql> select * from class A left join score B on A.class_id=B.class_id;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
| 2 | 数学 | B | 2 | A001 | 95 |
| 1 | 语文 | A | 1 | A002 | 82 |
| 2 | 数学 | B | 2 | A002 | 87 |
| 3 | 英语 | C | 3 | B003 | 65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and 1=1;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
| 2 | 数学 | B | 2 | A001 | 95 |
| 1 | 语文 | A | 1 | A002 | 82 |
| 2 | 数学 | B | 2 | A002 | 87 |
| 3 | 英语 | C | 3 | B003 | 65 |
+----------+------------+-------------+----------+--------+-------+
5 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql> select * from class A left join score B on 1=0;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql> left join的最重要特点是:不管on后面是什么条件,都会返回左表中的所有行!
mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
| 1 | 语文 | A | 1 | A002 | 82 |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 2 | 数学 | B | 2 | A001 | 95 |
| 2 | 数学 | B | 2 | A002 | 87 |
| 1 | 语文 | A | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
4 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 3 | 英语 | C | 3 | B003 | 65 |
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql>

如果on后面的条件是左表中的列(and leftTable.colName='***'),左表中满足条件的行和右表中的行进行匹配(根据on leftTable.id=rightTable.id);左表中不满足条件的行,直接输出,其对应的右表中的列都是null。

mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and B.Score=65;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 3 | 英语 | C | 3 | B003 | 65 |
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec) mysql>

如果on后面的条件是右表中的列(and rightTable.colName='***'),首先会根据(and rightTable.colName='***')过滤掉右表中不满足条件的行;然后,左表中的行根据(on leftTable.id=rightTable.id)和右表中满足条件的行进行匹配。

mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=90;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id and A.class_name='体育' and B.Score=82;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | NULL | NULL | NULL |
| 2 | 数学 | B | NULL | NULL | NULL |
| 3 | 英语 | C | NULL | NULL | NULL |
+----------+------------+-------------+----------+--------+-------+
3 rows in set (0.00 sec) mysql>

/**********************过滤条件在on中时**********************/
总结一下,如果 left join on leftTable.id=rightTable.id 后还有其他条件:
(1)and leftTable.colName='***',过滤左表,但是左表不满足条件的行直接输出,并将右表对应部分置为null
(2)and rightTable.colName='***',过滤右表,对左表没有影响
(3)and leftTable.colName='***' and rightTable.colName='***',就是上面(1)和(2)一起发挥作用

不管on后面有哪些条件,left join都要返回左表中的所有行!

mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
| 1 | 语文 | A | 1 | A002 | 82 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='数学';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 2 | 数学 | B | 2 | A001 | 95 |
| 2 | 数学 | B | 2 | A002 | 87 |
+----------+------------+-------------+----------+--------+-------+
2 rows in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='英语';
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 3 | 英语 | C | 3 | B003 | 65 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育';
Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=90;
Empty set (0.01 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=90;
Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='语文' and B.Score=91;
+----------+------------+-------------+----------+--------+-------+
| class_id | class_name | class_grade | class_id | stu_id | Score |
+----------+------------+-------------+----------+--------+-------+
| 1 | 语文 | A | 1 | A001 | 91 |
+----------+------------+-------------+----------+--------+-------+
1 row in set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=90;
Empty set (0.00 sec) mysql> select * from class A left join score B on A.class_id=B.class_id where A.class_name='体育' and B.Score=91;
Empty set (0.00 sec) mysql>

/**********************过滤条件在where中时**********************/

过滤条件写在where中时,先执行left join,然后再根据where条件进行过滤

sql中的left join以及on、where关键字的区别的更多相关文章

  1. Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式

    当前有两个表,sgroup与sgroupuser,两者通过gKey关联,而sgroup表记录的是组,而sgroupuser记录是组中的用户,因此在sgroupuser中不一定有数据.需要使用Left ...

  2. Spark SQL中出现 CROSS JOIN 问题解决

    Spark SQL中出现 CROSS JOIN 问题解决 1.问题显示如下所示:     Use the CROSS JOIN syntax to allow cartesian products b ...

  3. SQL中条件放在on后与where后的区别

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

  4. SQL 中的一些小巧但常用的关键字

    前面的几篇文章中,我们大体上介绍了 SQL 中基本的创建.查询语句,甚至也学习了相对复杂的连接查询和子查询,这些基本功相信你也一定掌握的不错,那么本篇则着重介绍几个技巧方面的关键字,能够让你更快更有效 ...

  5. sql中的inner join ,left join ,right join

    左连接LEFT JOIN, 也就是说,左外连接的含义是限制连接关键字右端的表中的数据必须满足连接条件,而不关左端的表中的数据是否满足连接条件,均输出左端表中的内容.不满足连接条件的 ,连接字段栏位将对 ...

  6. 关于Linq to Sql 中的left join 中defaultifempty的相关注意事项

    在使用Linq to Sql的时候,进行两个表的左连接的时候要注意defaultifempty的使用,这个函数本来的意思即是:如果为空则使用默认值代替,默认值为 NULL ,当然也可以使用defaul ...

  7. 关于SQL中Union和Join的用法

    转自帘卷西风的专栏(http://blog.csdn.net/ljxfblog) https://blog.csdn.net/ljxfblog/article/details/52066006 Uni ...

  8. sql中的inner join, left join, right join的区别

    下面介绍一下 inner join, left join, right join这者之间的区别 现在我假设有A表和B表 left join select * from A a left join B ...

  9. SQL中char、varchar、nvarchar、ntext的区别(转载)

    char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.nvarchar(n ...

随机推荐

  1. gradle根据不同渠道设置不同的开屏启动页

    需求:根据不同渠道,app的开屏启动页不一样 思路:因为app的启动页是在清单文件配置的,而清单文件最后是要和main里面的清单文件合并的,所以每个渠道都要配一个清单文件,在里面设置 然后在Andro ...

  2. vue组件通信那些事儿

    一.说说通信 通信,简言之,交流信息.交流结束后,把信息放在哪里,这是一个值得思考的问题.vue中保存状态有2种方式,组件内的data属性和组件外的vuex的state属性. 1.用state的场景 ...

  3. UVA 277 Puzzle

    题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...

  4. BZOJ.5289.[AHOI/HNOI2018]排列(贪心 heap)

    BZOJ LOJ 洛谷 \(Kelin\)写的挺清楚的... 要求如果\(a_{p_j}=p_k\),\(k\lt j\),可以理解为\(k\)要在\(j\)之前选. 那么对于给定的\(a_j=k\) ...

  5. 潭州课堂25班:Ph201805201 爬虫高级 第十三 课 代理池爬虫检测部分 (课堂笔记)

    1,通过爬虫获取代理 ip ,要从多个网站获取,每个网站的前几页2,获取到代理后,开进程,一个继续解析,一个检测代理是否有用 ,引入队列数据共享3,Queue 中存放的是所有的代理,我们要分离出可用的 ...

  6. tableviewcell选中不变色。

    tableview 选中一行后,不显示选中颜色 添加这样一句话就好 cell.selectionStyle = UITableViewCellSelectionStyleNone; 一定不要table ...

  7. sklearn逻辑回归

    sklearn逻辑回归 logistics回归名字虽然叫回归,但实际是用回归方法解决分类的问题,其形式简洁明了,训练的模型参数还有实际的解释意义,因此在机器学习中非常常见. 理论部分 设数据集有n个独 ...

  8. oval-and-rectangle

    oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. eclipse中安装pydev插件出现duplicate location

    eclipse中安装pydev插件出现duplicate location,主要是因为之前已经填写了该地址并且已经加载了,具体的解决办法见下链接: http://jingyan.baidu.com/a ...

  10. python 绘图pylab

    绘图 1.绘图主要通过代码来进行认知 demo: import pylab #绘图库 pylab.figure(1) #创建图一 pylab.plot([1,2,3,4],[1,2,3,4]) #在图 ...