談談oracle里的join、left join、right join、full join-版本2



--1.left join  左表為主表,左表返回全部數據,右表只返回與左表相匹配的數據
select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fpdm,t2.fphm,t2.zflx  from yw_zjfpjl t1
left join xxdzmx t2  on t2.fpdm||t2.fphm=t1.fpdm||t1.fphm

select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fpdm,t2.fphm,t2.zflx  from yw_zjfpjl t1 , xxdzmx t2  where t2.fphm=t1.fphm(+)

--2.right join 右表為主表,右表返回全部數據,左表只返回與右表相匹配的數據
select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fpdm,t2.fphm,t2.zflx  from yw_zjfpjl t1
right join xxdzmx t2  on t2.fpdm||t2.fphm=t1.fpdm||t1.fphm

--3.inner join  只返回兩表相匹配的數據
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';

--4.full join
--4.1全部結果集  106
select   t1.fpdm,t1.fphm ,t1.zjr,t1.zjsj,t1.zjjx,t1.zjje,t1.zflx,t1.zfid,t2.fpdm,t2.fphm,t2.zflx  from yw_zjfpjl t1
full join xxdzmx t2  on t2.fpdm=t1.fpdm and t2.fphm=t1.fphm

--4.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'  

--4.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'  );

--4.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'  );



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM