Home > Database > Mysql Tutorial > oracle中left join和right join的区别浅谈

oracle中left join和right join的区别浅谈

WBOY
Release: 2016-06-07 17:56:00
Original
1128 people have browsed it

oracle中left join和right join的区别浅谈,需要的朋友可以参考一下

通俗的讲:

A left join B 的连接的记录数与A表的记录数同

A right join B 的连接的记录数与B表的记录数同

A left join B 等价B right join A

table A:

Field_K, Field_A

1 a

3 b

4 c

table B:

Field_K, Field_B

1 x

2 y

4 z

select a.Field_K, a.Field_A, b.Field_K, b.Field_B

from a left join b on a.Field_K=b.Field_K

Field_K Field_A Field_K Field_B

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

1 a 1 x

3 b NULL NULL

4 c 4 z

select a.Field_K, a.Field_A, b.Field_K, b.Field_B

from a right join b on a.Field_K=b.Field_K

Field_K Field_A Field_K Field_B

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

1 a 1 x

NULL NULL 2 y

4 c 4 z --

举个例子:

假设a表和b表的数据是这样的。

a b

id name  id stock 

1  a 1 15

2 b 2 50

3 c  

select * from a inner join b on a.id=b.id

这个语法是连接查询中的内连接,它产生的结果是

两个表相匹配的记录出现在结果列表中。

根据上面的表,出现的结果是这样的

a.id name b.id stock

1   a 1 15

2 b 2 50

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

select * from a,b where a.id=b.id

这个语法是内连接的另外一种写法,其执行结果与inner join 一样

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

select * from a left/right join b on a.id=b.id

这个是外连接语法中的左外连接或右外连接

如果是左外连接的话,它将显示a表的所有记录,

select a.*,b.* from a left join b on a.id=b.id

查询的结果是这样的:

a.id name b.id stock

1   a 1 15

2 b 2 50

3 c null null 

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

如果是右外连接的话,它将显示b表的所有记录,

select a.*,b.* from a right join b on a.id=b.id

查询的结果是这样的:

a.id name b.id stock

1   a 1 15

2 b 2 50

--

select a.*,b.* from a left join b on a.k = b.k

select a.*,b.* from a left outer join b on a.k =b.k

----------上面两种一样left join是left outer join的简写

select a.*,b.* from a left inner join b on a.k = b.k

没有这种写法,错误的语句.

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