准备工作
oracle连接分为:
连接的语法:
- left/right/full outer join ...on
- left/right/full join ...on
(+)号的作用:+号可以理解为补充的意思,加在那个表的列上就代表这个表的列为补充。加在右表的列上代表右表为补充,为左连接。加在左表的列上代表左表为补充,为右连接。注意:完全外连接中不能使用+号。
创建两种表,生出测试数据:
CREATE TABLE t_A ( id number,name VARCHAR2(10) ); CREATE TABLE t_B ( id number,name VARCHAR2(10) ); INSERT INTO t_A VALUES(1,'A'); INSERT INTO t_A VALUES(2,'B'); INSERT INTO t_A VALUES(3,'C'); INSERT INTO t_A VALUES(4,'D'); INSERT INTO t_A VALUES(5,'E'); INSERT INTO t_B VALUES(1,'AA'); INSERT INTO t_B VALUES(1,'BB'); INSERT INTO t_B VALUES(2,'CC'); INSERT INTO t_B VALUES(1,'DD'); INSERT INTO t_B VALUES(7,'GG');
左外连接
select * from t_a a left join t_b b on a.id=b.id; select * from t_a a,t_b b where a.id=b.id(+);
右外连接
select * from t_a a right join t_b b on a.id = b.id; select * from t_a a,t_b b where a.id(+)=b.id;
完全外连接
select * from t_a a full join t_b b on a.id=b.id;
等值连接(我们在看看等值连接的结果)
select * from t_a a,t_b b where a.id=b.id; select * from t_a a join t_b b on a.id=b.id;--等值连接也可以这样写