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 |
+----------+-----------+----------+-----------+---------------+-------+
最后注意一下,下面等号左右两边的关键字是等价的:
1
2
3
4
|
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小结内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/grandyang/p/5346963.html