Home > Database > Mysql Tutorial > 数据库 left join(或者left outer join),right join(或者right o

数据库 left join(或者left outer join),right join(或者right o

WBOY
Release: 2016-06-07 15:30:11
Original
1511 people have browsed it

sql连接共三种:内连接,外连接,交叉连接。 内连接包含:等连接,不等连接,自然连接 外连接包含:左连接(左外连接),右连接(右外连接) 具体理论见我的博文http://blog.csdn.net/jdfkldjlkjdl/article/details/41485127 -------------------------------------

sql连接共三种:内连接,外连接,交叉连接。

内连接包含:等值连接,不等值连接,自然连接

外连接包含:左连接(左外连接),右连接(右外连接)

具体理论见我的博文http://blog.csdn.net/jdfkldjlkjdl/article/details/41485127

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下面是实例介绍,相信这样更直观。

现有A和B两个表

表A记录如下: 
aID         aNum 
1            a20050111 
2            a20050112 
3            a20050113 
4            a20050114 
5            a20050115 
表B记录如下: 
bID         bName 
1            2006032401 
2            2006032402 
3            2006032403 
4            2006032404 
8            2006032408 
1.left join(左联接) 
SELECT * FROM a LEFT JOIN b ON a.aID =b.bID 
结果如下: 
aID              aNum              bID            bName 
1                 a20050111     1               2006032401 
2                 a20050112     2               2006032402 
3                 a20050113     3               2006032403 
4                 a20050114     4               2006032404 
5                 a20050115    NULL         NULL 
(所影响的行数为 5 行) 
结果说明: 
left join是以A表的记录为基础的,A可以看成左表,B可以看成右表,left join是以左表为准的. 
换句话说,左表(A)的记录将会全部表示出来,而右表(B)只会显示符合搜索条件的记录(例子中为: A.aID = B.bID). B表记录不足的地方均为NULL. 
2.right join(右联接) 
SELECT * FROM a RIGHT JOING b ON a.aID = b.bID 
结果如下: 
aID              aNum              bID            bName 
1                 a20050111     1               2006032401 
2                 a20050112     2               2006032402 
3                 a20050113     3               2006032403 
4                 a20050114     4               2006032404 
NULL           NULL              8               2006032408 
(所影响的行数为 5 行) 
结果说明: 
仔细观察一下,就会发现,和left join的结果刚好相反,这次是以右表(B)为基础的,A表不足的地方用NULL填充. 
3.inner join(相等联接或内联接) 
SELECT * FROM a INNER JOIN b ON a.aID =b.bID 
等同于以下SQL句: 
SELECT * FROM a,b WHERE a.aID = b.bID 
结果如下: 
aID             aNum              bID            bName 
1                a20050111     1                2006032401 
2                a20050112     2                2006032402 
3                a20050113     3                2006032403 
4                a20050114     4                2006032404 
结果说明: 
很明显,这里只显示出了 A.aID = B.bID的记录.这说明inner join并不以谁为基础,它只显示符合条件的记录.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template