博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结
阅读量:6296 次
发布时间:2019-06-22

本文共 5845 字,大约阅读时间需要 19 分钟。

在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.PersonIdUNIONSELECT * 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 BA RIGHT JOIN B     =       A RIGHT OUTER JOIN BA FULL JOIN B      =       A FULL OUTER JOIN BA INNER JOIN B     =       A JOIN B

本文转自博客园Grandyang的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
Propel项目改为基于TensorFlow.js
查看>>
Azure正式对外发布容器服务,支持Swarm和Mesos
查看>>
阿里巴巴收购以色列VR公司,大厂死磕VR为哪般?
查看>>
埃隆·马斯克:比特币拥有着“极为出色”的结构,而纸质货币终将消失
查看>>
如何用度量影响敏捷环境
查看>>
Facebook使用机器学习手段来自动优化其系统性能
查看>>
借助Unity AR Foundation构建跨平台AR应用
查看>>
Kubernetes 落地案例|使用 Kubernetes 重新部署全球最大的教育公司
查看>>
手工测试对比自动化测试
查看>>
vue.js快速入门
查看>>
浅论服务端应用程序开发中的CAP思想(非分布式系统中的CAP理论)
查看>>
socket.io的 Python客户端中文encode问题
查看>>
[LintCode] Shape Factory
查看>>
html5手机页面的那些meta
查看>>
最长回文子串问题
查看>>
SpringBoot配置属性之MQ
查看>>
0x01 念念Python,必有回响
查看>>
Vim实战指南(一):基础编辑命令
查看>>
学习 PHP SOAP 扩展的一些笔记
查看>>
PHP SOAP 扩展的使用
查看>>