SQL之各种join小结详细讲解
来源:脚本之家
时间:2022-12-31 14:10:40 450浏览 收藏
本篇文章向大家介绍《SQL之各种join小结详细讲解》,主要包括SQLjoin,具有一定的参考价值,需要的朋友可以参考一下。
SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
在SQL语言中,存在着各种Join,有Left Join, Right Join, Inner Join, and Natural 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小结详细讲解》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
184 收藏
-
237 收藏
-
210 收藏
-
192 收藏
-
364 收藏
-
373 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习
-
- 活泼的红酒
- 这篇技术贴太及时了,楼主加油!
- 2023-01-29 17:26:51
-
- 害怕的店员
- 这篇文章真是及时雨啊,太全面了,很好,收藏了,关注师傅了!希望师傅能多写数据库相关的文章。
- 2023-01-19 21:40:22
-
- 爱笑的大侠
- 感谢大佬分享,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢大佬分享博文!
- 2023-01-18 20:46:17
-
- 典雅的服饰
- 好细啊,已加入收藏夹了,感谢楼主的这篇技术贴,我会继续支持!
- 2023-01-18 15:18:07
-
- 俊秀的保温杯
- 这篇文章内容真是及时雨啊,作者大大加油!
- 2023-01-03 04:17:37
-
- 重要的芹菜
- 感谢大佬分享,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢作者大大分享技术文章!
- 2023-01-03 01:09:04
-
- 傻傻的玫瑰
- 细节满满,已收藏,感谢作者的这篇博文,我会继续支持!
- 2023-01-01 14:45:10
-
- 孝顺的红牛
- 这篇博文出现的刚刚好,太全面了,真优秀,码起来,关注作者大大了!希望作者大大能多写数据库相关的文章。
- 2023-01-01 12:47:06