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

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. 将linux系统目录挂载到其他分区,扩大系统可用空间

    刚看到有小白用户说linux系统盘分区太小,不够用,问是不是要重装系统? 其实是不需要重装系统的,可以考虑把一些系统目录挂载到单独的分区. 比如将用户目录 /home 挂载到单独的分区: 1.首先打开 ...

  2. Linux下发送邮件

    Linux下发送邮件 1.配置 vim /etc/mail.rc 文件尾增加以下内容 set from=ymwugui@linuxidc.com smtp=smtp.sina.com.cn set s ...

  3. hdu3944

    hdu3944题目中给出的杨辉三角形的形状带有误导目的,应该转化成对称的形状再去思考这个问题分两种情况第一个是在左区从目标位置向左上方走一直走到边界,然后再向右上方一直走到起点n-k个1加上C(n-k ...

  4. Python import错误

    今天将一个文件命名为select,在其中import了  A文件 再然后发现 B文件import A文件,会将select也一起运行. 还出现的问题是 A中的类实例的时候说找不到. 最后想到selec ...

  5. shell crlf to lf

    UNIX/Linux Commands You can use the following tools: dos2unix (also known as fromdos) – converts tex ...

  6. AS安装过程中出现的错误

    1.首先是You may need to adjust the proxy settings in Gradle.的错误, 主要是看你有没有图中红线所画的gradle的压缩包 如果没有,那就前往htt ...

  7. 字节跳动冬令营网络赛 D.The Easiest One(贪心 数位DP)

    题目链接 \(x:\ 11010011\) \(y:\ 10011110\) (下标是从高位往低位,依次是\(1,2,...,n\)) 比如对于这两个数,先找到最高的满足\(x\)是\(0\),\(y ...

  8. 洛谷.3808/3796.[模板]AC自动机

    题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...

  9. OI考试需注意的

    能用结构体就用结构体,特别是队列之类的数据结构:类别相同的变量或数组名字不要太相近,最好在名字后面加上标识符(e.g:hash[]&HASH[]就不好,hash1[]&hash2[]正 ...

  10. SpringMVC Controller之间的重定向和转发

    同一个controller之间重定向和转发 ①redirect 在Controller的映射方法中,其返回值改为:return "redirect:XXX"; ②forward 这 ...