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 自定义变量的概念及特点
May 13 MySQL
MySQL 全文索引使用指南
May 25 MySQL
SQL实现LeetCode(176.第二高薪水)
Aug 04 MySQL
mysql 联合索引生效的条件及索引失效的条件
Nov 20 MySQL
VS2019连接MySQL数据库的过程及常见问题总结
Nov 27 MySQL
MySQL笔记 —SQL运算符
Jan 18 MySQL
解决MySQL添加新用户-ERROR 1045 (28000)的问题
Mar 03 MySQL
面试中老生常谈的MySQL问答集锦夯实基础
Mar 13 MySQL
mysql的单列多值存储实例详解
Apr 05 MySQL
MySQL中一条SQL查询语句是如何执行的
Apr 08 MySQL
Mysql将字符串按照指定字符分割的正确方法
May 30 MySQL
Mysql表数据比较大情况下修改添加字段的方法实例
Jun 28 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
我的论坛源代码(四)
2006/10/09 PHP
搜索和替换文件或目录的一个好类--很实用
2006/10/09 PHP
PHP 开源AJAX框架14种
2009/08/24 PHP
十幅图告诉你什么是PHP引用
2015/02/22 PHP
PHP使用反射机制实现查找类和方法的所在位置
2016/04/22 PHP
PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
2017/05/17 PHP
PHP用continue跳过本次循环中剩余代码的注意点
2017/06/27 PHP
window.open的功能全解析
2006/10/10 Javascript
单击复制文字兼容各浏览器的完美解决方案
2013/07/04 Javascript
JS小游戏之象棋暗棋源码详解
2014/09/25 Javascript
深入浅析AngularJS和DataModel
2016/02/16 Javascript
JS中的二叉树遍历详解
2016/03/18 Javascript
jQuery前端开发35个小技巧
2016/05/24 Javascript
原生js实现手风琴功能(支持横纵向调用)
2017/01/13 Javascript
基于vue.js实现侧边菜单栏
2017/03/20 Javascript
解决vue里碰到 $refs 的问题的方法
2017/07/13 Javascript
node.js express中app.param的用法详解
2017/07/16 Javascript
vue监听scroll的坑的解决方法
2017/09/07 Javascript
JavaScript中立即执行函数实例详解
2017/11/04 Javascript
Angular4 ElementRef的应用
2018/02/26 Javascript
分享8个JavaScript库可更好地处理本地存储
2020/10/12 Javascript
Python判断列表是否已排序的各种方法及其性能分析
2016/06/20 Python
Python创建xml文件示例
2017/03/22 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
2018/01/29 Python
pyhanlp安装介绍和简单应用
2019/02/22 Python
python使用adbapi实现MySQL数据库的异步存储
2019/03/19 Python
python3实现小球转动抽奖小游戏
2020/04/15 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
Python实现京东抢秒杀功能
2021/01/25 Python
普天C++笔试题
2016/03/20 面试题
文明倡议书范文
2014/04/15 职场文书
室内设计专业毕业生求职信
2014/05/02 职场文书
2014年双拥工作总结
2014/11/21 职场文书
搞笑老公保证书
2015/02/26 职场文书
原来闭幕词是这样写的呀!
2019/07/01 职场文书
中秋节作文(五年级)之关于月亮
2019/09/11 职场文书