Oracle 左右连接总结
--建立测试数据
create table a(id number);
create table b(id number);
insert into a values(1);
insert into a values(2);
insert into a values(3);
insert into b values(1);
insert into b values(2);
insert into b values(4);
commit;
create table a(id number);
create table b(id number);
insert into a values(1);
insert into a values(2);
insert into a values(3);
insert into b values(1);
insert into b values(2);
insert into b values(4);
commit;
--左连接;
--主流数据库通用的方法
select * from a left join b on a.id = b.id;
--Oracle特有的方法
select * from a, b where a.id = b.id(+)
--结果
ID ID
1 1 1
2 2 2
3 3
--主流数据库通用的方法
select * from a left join b on a.id = b.id;
--Oracle特有的方法
select * from a, b where a.id = b.id(+)
--结果
ID ID
1 1 1
2 2 2
3 3
--右连接:
--主流数据通用的方法
select * from a right join b on a.id = b.id;
--Oracle特有的方法
select * from a, b where a.id(+) = b.id
--结果
ID ID
1 1 1
2 2 2
3 4
--主流数据通用的方法
select * from a right join b on a.id = b.id;
--Oracle特有的方法
select * from a, b where a.id(+) = b.id
--结果
ID ID
1 1 1
2 2 2
3 4
--内连接:
--主流数据库通用的方法
select * from a join b on a.id=b.id
--where关联
select * from a ,b where a.id=b.id
--结果
ID ID
1 1 1
2 2 2
--主流数据库通用的方法
select * from a join b on a.id=b.id
--where关联
select * from a ,b where a.id=b.id
--结果
ID ID
1 1 1
2 2 2
--全外连接:
--主流数据库通用的方法
select * from a full join b on a.id =b.id
--Oracle特有的方法
select * from a,b where a.id =b.id(+)
union
select * from a,b where a.id(+) =b.id
--结果
ID ID
1 1 1
2 2 2
3 3
4 4
--完全连接,也叫交叉连接或者笛卡尔积
--主流数据库通用的方法
select * from a,b;
--或者
select * from a cross join b;
--结果
ID ID
1 1 1
2 1 2
3 1 4
4 2 1
5 2 2
6 2 4
7 3 1
8 3 2
9 3 4
--主流数据库通用的方法
select * from a,b;
--或者
select * from a cross join b;
--结果
ID ID
1 1 1
2 1 2
3 1 4
4 2 1
5 2 2
6 2 4
7 3 1
8 3 2
9 3 4