SQL中left join后面on、where的区别

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

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);

  比较下面几组查询结果:

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)

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)

left join的最重要特点是:不管on后面是什么条件,都会返回左表中的所有行!

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)

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)

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)

如果on后面的条件是左表中的列,那么左表中满足条件的行和右表中的行进行匹配,左表中不满足条件的行其右表中为Null。
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)

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)

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

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)

/**********************过滤条件在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都要返回左表中的所有行!

 

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

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



posted on 2019-03-28 20:14  Muliti_Hu  阅读(1122)  评论(0编辑  收藏  举报

导航