SQL之各种join小结详细讲解


Posted in MySQL onAugust 04, 2021

SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结

在SQL语言中,存在着各种Join,有Left Join, Right Join, Inner Join, and Natural Join等,对于初学者来说肯定一头雾水,都是神马跟神马啊,它们之间到底有着怎样的区别和联系呢,我们先用一张图片来说明:

SQL之各种join小结详细讲解

上面这张图很好的阐释了Left Join, Right Join, Inner Join,和Full Outer Join的区别,下面用我们用一个简单的例子来帮助我们理解和区分,现在有两个表Person和Address:

-- Table Person
+----------+-----------+----------+
| PersonId | FirstName | LastName |
+----------+-----------+----------+
|        1 | Zhang     | San      |
|        2 | Li        | Si       |
|        3 | Wang      | Wu       |
|        4 | Yang      | Liu      |
+----------+-----------+----------+


-- Table Address
+-----------+----------+---------------+-------+
| AddressId | PersonId | City          | State |
+-----------+----------+---------------+-------+
|         1 |        2 | San Francisco | CA    |
|         2 |        3 | Los Angeles   | CA    |
|         3 |        1 | San Diego     | CA    |
+-----------+----------+---------------+-------+

我们下面一个一个的来看:

Left Join: returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

左交:返回左表的所有行和匹配的右表的行,如果没有匹配上的用NULL.

SELECT * FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId; 
+----------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City   | State |
+----------+-----------+----------+-----------+----------+---------------+-------+
|        2 | Li        | Si       |         1 |        2 | San Francisco | CA    |
|        3 | Wang      | Wu       |         2 |        3 | Los Angeles   | CA    |
|        1 | Zhang     | San      |         3 |        1 | San Diego     | CA    |
|        4 | Yang      | Liu      |      NULL |     NULL | NULL          | NULL  |
+----------+-----------+----------+-----------+----------+---------------+-------+

Right Join: returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

右交:返回右表的所有行和匹配的左表的行,如果没有匹配上的用NULL.

SELECT * FROM Person RIGHT JOIN Address ON Person.PersonId = Address.PersonId; 
+----------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City          | State |
+----------+-----------+----------+-----------+----------+---------------+-------+
|        1 | Zhang     | San      |         3 |        1 | San Diego     | CA    |
|        2 | Li        | Si       |         1 |        2 | San Francisco | CA    |
|        3 | Wang      | Wu       |         2 |        3 | Los Angeles   | CA    |
|     NULL | NULL      | NULL     |         4 |        5 | Memphis       | TN    |
+----------+-----------+----------+-----------+----------+---------------+-------+

Inner Join: selects all rows from both tables as long as there is a match between the columns in both tables.

内交: 选择左右表中关键字匹配上的行。

SELECT * FROM Person INNER JOIN Address ON Person.PersonId = Address.PersonId;  +----------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City          | State |
+----------+-----------+----------+-----------+----------+---------------+-------+
|        1 | Zhang     | San      |         3 |        1 | San Diego     | CA    |
|        2 | Li        | Si       |         1 |        2 | San Francisco | CA    |
|        3 | Wang      | Wu       |         2 |        3 | Los Angeles   | CA    |
+----------+-----------+----------+-----------+----------+---------------+-------+

Full Join: returns all rows from the left table (table1) and from the right table (table2), and it combines the result of both LEFT and RIGHT joins.

全交: 返回左表的所有行和右表的所有行,是左交和右交的联合。

注意,由于MySql中没有Full Join命令,所以我们通过把Left Join和Right Join的结果Union起来也是可以的:

SELECT * FROM Person LEFT JOIN Address ON Person.PersonId = Address.PersonId UNION
SELECT * FROM Person RIGHT JOIN Address ON Person.PersonId = Address.PersonId;  +----------+-----------+----------+-----------+----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | PersonId | City          | State |
+----------+-----------+----------+-----------+----------+---------------+-------+
|        2 | Li        | Si       |         1 |        2 | San Francisco | CA    |
|        3 | Wang      | Wu       |         2 |        3 | Los Angeles   | CA    |
|        1 | Zhang     | San      |         3 |        1 | San Diego     | CA    |
|        4 | Yang      | Liu      |      NULL |     NULL | NULL          | NULL  |
|     NULL | NULL      | NULL     |         4 |        5 | Memphis       | TN    |
+----------+-----------+----------+-----------+----------+---------------+-------+

Natural Join: creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join. The default is INNER join.

自然交: 根据左右两表的相同列创建一个隐含的join操作,相同列就是两表中列名相同的两列。自然交可以是内交,左交或者是右交。默认是内交。

SELECT * FROM Person NATURAL JOIN Address; 

+----------+-----------+----------+-----------+---------------+-------+
| PersonId | FirstName | LastName | AddressId | City          | State |
+----------+-----------+----------+-----------+---------------+-------+
|        1 | Zhang    | San      |         3 | San Diego     | CA    |
|        2 | Li        | Si       |         1 | San Francisco | CA    |
|        3 | Wang      | Wu       |         2 | Los Angeles   | CA    |
+----------+-----------+----------+-----------+---------------+-------+

最后注意一下,下面等号左右两边的关键字是等价的:

A LEFT JOIN B      =       A LEFT OUTER JOIN B
A RIGHT JOIN B     =       A RIGHT OUTER JOIN B
A FULL JOIN B      =       A FULL OUTER JOIN B
A INNER JOIN B     =       A JOIN B

到此这篇关于SQL之各种join小结详细讲解的文章就介绍到这了,更多相关SQL之各种join小结内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

MySQL 相关文章推荐
MySQL分库分表与分区的入门指南
Apr 22 MySQL
详解Mysql和Oracle之间的误区
May 18 MySQL
MySQL 数据类型选择原则
May 27 MySQL
MySQL CHAR和VARCHAR该如何选择
May 31 MySQL
详细谈谈MYSQL中的COLLATE是什么
Jun 11 MySQL
python中的mysql数据库LIKE操作符详解
Jul 01 MySQL
MySQL定时备份数据库(全库备份)的实现
Sep 25 MySQL
SQL基础查询和LINQ集成化查询
Jan 18 MySQL
mysql 生成连续日期及变量赋值
Mar 20 MySQL
MySQL实战记录之如何快速定位慢SQL
Mar 23 MySQL
MySQL数据库优化之通过索引解决SQL性能问题
Apr 10 MySQL
MySQL 执行数据库更新update操作的时候数据库卡死了
May 02 MySQL
SQL实现LeetCode(175.联合两表)
Aug 04 #MySQL
一篇文章带你深入了解Mysql触发器
Aug 02 #MySQL
sql注入教程之类型以及提交注入
MySQL子查询中order by不生效问题的解决方法
Aug 02 #MySQL
MySQL中utf8mb4排序规则示例
Aug 02 #MySQL
MySql子查询IN的执行和优化的实现
MySQL里面的子查询的基本使用
Aug 02 #MySQL
You might like
php生成SessionID和图片校验码的思路和实现代码
2009/03/10 PHP
input file获得文件根目录简单实现
2013/04/26 PHP
解析php如何将日志写进syslog
2013/06/28 PHP
php中自定义函数dump查看数组信息类似var_dump
2014/01/27 PHP
php面向对象中static静态属性和静态方法的调用
2015/02/08 PHP
yii框架redis结合php实现秒杀效果(实例代码)
2017/10/26 PHP
Thinkphp5.0框架视图view的循环标签用法示例
2019/10/12 PHP
Javascript MD4
2006/12/20 Javascript
JS实现仿新浪微博发布内容为空时提示功能代码
2015/08/19 Javascript
JavaScript各类型的关系图解
2015/10/16 Javascript
Extjs4.0 ComboBox如何实现三级联动
2016/05/11 Javascript
AngularJS的脏检查深入分析
2017/04/22 Javascript
详解使用angular-cli发布i18n多国语言Angular应用
2017/05/20 Javascript
javascript+html5+css3自定义提示窗口
2017/06/21 Javascript
移动设备手势事件库Touch.js使用详解
2017/08/18 Javascript
基于JavaScript实现评论框展开和隐藏功能
2017/08/25 Javascript
JS实现标签滚动切换效果
2017/12/25 Javascript
官方推荐react-navigation的具体使用详解
2018/05/08 Javascript
vue动态绑定组件子父组件多表单验证功能的实现代码
2018/05/14 Javascript
利用JavaScript缓存远程窃取Wi-Fi密码的思路详解
2018/11/05 Javascript
JS猜数字游戏实例讲解
2020/06/30 Javascript
Vue 实现对quill-editor组件中的工具栏添加title
2020/08/03 Javascript
python实现可以断点续传和并发的ftp程序
2016/09/13 Python
python自定义异常实例详解
2017/07/11 Python
pygame游戏之旅 如何制作游戏障碍
2018/11/20 Python
Python魔法方法功能与用法简介
2019/04/04 Python
Python生成一个迭代器的实操方法
2019/06/18 Python
使用selenium和pyquery爬取京东商品列表过程解析
2019/08/15 Python
python中如何打包用户自定义模块
2020/09/23 Python
加州风格的游泳和沙滩装品牌:Cupshe
2019/06/10 全球购物
JNI的定义
2012/11/25 面试题
六十大寿答谢词
2014/01/12 职场文书
入职担保书范文
2014/05/21 职场文书
护士辞职信怎么写
2015/02/27 职场文书
学生会招新宣传语
2015/07/13 职场文书
i5-10400f处理相当于i7多少水平
2022/04/19 数码科技