欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

sql中的left join以及on、where关键字的区别

程序员文章站 2023-10-27 22:28:28
创建两张表并插入一些数据 查看表中的数据 比较下面几组查询结果--如果你对left join足够熟悉的话,先不要看结果,是否可以直接说出下面查询的结果 如果on后面的条件是左表中的列(and leftTable.colName='***'),左表中满足条件的行和右表中的行进行匹配(根据on left ......

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

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中时,先根据where条件对表进行过滤,然后再执行left join