谈谈oracle里的join、left join、right join、full join

 

谈谈oracle里的joinleft joinright joinfull join

 

 

示例表

create table l as
select 'left_1' as str,'1' as v from dual union all
select 'left_2' ,'2' as v from dual union all
select 'left_3' ,'3' as v from dual union all
select 'left_4' ,'4' as v from dual ;

create table r as
select 'right_3' as str,'3' as v, 1  as status from dual union all
select 'right_4' as str,'4' as v, 0  as status from dual union all
select 'right_5' as str,'5' as v, 0  as status from dual union all
select 'right_6' as str,'6' as v, 0  as status from dual ;

select * from l; --where l.v not in (select r.v from r )

select * from r

1. inner join

返回两表相匹配的数据  交集
select l.str as left_str,r.str as right_str from l
inner join r on l.v=r.v
order by 1,2;

select l.str as left_str,r.str rigth_str from l,r
where l.v=r.v
order by 1,2;

2.left join 

左表为主表,左表返回全部数据,右表只返回与左表相匹配的数据
select l.str as left_str,r.str right_str from l
left join r on l.v=r.v
order by 1,2;

select l.str as left_str,r.str as right_str from l,r
where l.v=r.v(+)
order by 1,2;

select l.str as left_str,r.str as right_str from l,r
where l.v=r.v
order by 1,2;

 

 3.right join

右表为主表,左表只返回与右表相匹配的数据
select l.str as left_str,r.str as right_str from l
right join r on l.v=r.v
order by 1,2;

select l.str as left_str,r.str right_str from l,r
where l.v(+)=r.v
order by 1,2;

4. Full join

左右表均返回全部的数据,但是只有相匹配的数据显示在一行,非匹配的数据只显示一个表的数据
select l.str as left_str,r.str as right_str from l
full join r on r.v=l.v
order by 1,2;

5.full join

5.1全部结果集  106

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fphm,t2.zflx  from yw_zjfpjl t1

full join xxdzmx t2  on t2.fpdm=t1.fpdm and t2.fphm=t1.fphm

 

5.2右边有左边没有   10-1=9

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fphm,t2.fpdm,t2.zflx,t2.dzyf  from yw_zjfpjl t1

full join xxdzmx t2  on t2.fphm||t2.fpDm = t1.fphm||t1.fpdm

where t2.fphm is not  null and t2.zflx =0 and substr(t2.dzyf,1 ,4)||'-'||substr(t2.dzyf,5,2)='2016-10'  ;

 

5.3左边有右边没有   97-1=96

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fphm,t2.fpdm,t2.zflx  from yw_zjfpjl t1

full join xxdzmx t2  on t2.fphm||t2.fpDm = t1.fphm||t1.fpdm

where  (t1.zflx='0' and TO_CHAR(t1.zjsj,'YYYY-MM')='2016-10'  );

 

5.4左边有右边没有  右边有左边没有  97-1+10-1=105

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fphm,t2.fpdm,t2.zflx,t2.dzyf  from yw_zjfpjl t1

full join xxdzmx t2  on t2.fphm||t2.fpDm = t1.fphm||t1.fpdm

where (t2.fphm is not  null and t2.zflx ='0' and substr(t2.dzyf,1 ,4)||'-'||substr(t2.dzyf,5,2)='2016-10') or (t1.zflx='0' and TO_CHAR(t1.zjsj,'YYYY-MM')='2016-10'  );

 

5.5 inner join  交集=1

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid, t2.fphm,t2.fpdm,t2.zflx  from yw_zjfpjl t1

inner join xxdzmx t2  on t2.fphm||t2.fpDm = t1.fphm||t1.fpdm

where t2.fphm is not null and t2.zflx='0' and t1.zflx='0' and TO_CHAR(t1.zjsj,'YYYY-MM')='2016-10'  and substr(t2.dzyf, 0, 4)||'-'||substr(t2.dzyf, 5, 2)='2016-10';

 

创建时间:2022-04-02 15:14
浏览量:0