侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

sql中的left join及on、where条件关键字的区别详解

数据库  /  管理员 发布于 5年前   326

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

LEFT JOIN 关键字语法

SELECT column_name(s)FROM table_name1LEFT 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 server deadlock跟踪的4种实现方法
    下一条:
    SqlServer GO命令循环使用实例代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客