• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

SQL之各种join小结详细讲解

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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小结详细讲解的文章就介绍到这了,更多相关SQL之各种join小结内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用Redis实现UA池的方案发布时间:2022-02-10
下一篇:
大家都应该知道的Redis过期键与过期策略发布时间:2022-02-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap