常春岛资源网 Design By www.syssdc.com

LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。

LEFT JOIN 关键字语法

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2 
ON table_name1.column_name=table_name2.column_name

注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。

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

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

总结

以上所述是小编给大家介绍的sql中的left join及on、where关键字的区别详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

标签:
sql,left,join,sql,on,sql,where

常春岛资源网 Design By www.syssdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
常春岛资源网 Design By www.syssdc.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。