Home > Database > Mysql Tutorial > sql语句right join用法详细介绍

sql语句right join用法详细介绍

WBOY
Release: 2016-06-07 17:49:27
Original
3583 people have browsed it

right join:是SQL语言中的查询类型,即连接查询。它的全称为右外连接( right outer join),是外连接的一种

表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


sql语句如下:

 代码如下 复制代码
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填充.

注意on与where区别

ON语句其实和WHERE语句功能大致相当,只是这里的ON语句是专门针对联结表的,ON语句后面的条件的要求和书写方式和WHERE语句的要求是一样的,大家基本上可以把ON当作WHERE用。

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