内连接、外连接和全连接的区别

举例说明

  • 假设您有两个表,每个表只有一个列,表数据如下
A    B
-    -
1    3
2    4
3    5
4    6

注意,(1,2)是A表唯一的,(3,4)是公共的,并且(5,6)是B表独有的

内连接

内连接是A表的所有行交上B表的所有行得出的结果集

select * from a INNER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a = b.b;

a | b
--+--
3 | 3
4 | 4

左外连接

左外连接是A表的所有行匹配上B表得出的结果集

select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a = b.b(+);

a |  b
--+-----
1 | null
2 | null
3 |    3
4 |    4

右外连接

右外连接是B表的所有行匹配上A表得出的结果集

select * from a RIGHT OUTER JOIN b on a.a = b.b;
select a.*, b.*  from a,b where a.a(+) = b.b;

a    |  b
-----+----
3    |  3
4    |  4
null |  5
null |  6

全连接

全连接是A表的所有行并上B表的所有行得出的结果集

select * from a FULL OUTER JOIN b on a.a = b.b;

 a   |  b
-----+-----
   1 | null
   2 | null
   3 |    3
   4 |    4
null |    6
null |    5

参考文档:

  1. https://stackoverflow.com/questions/38549/what-is-the-difference-between-inner-join-and-outer-join

你可能感兴趣的:(SQL)