在3个表上的SQL左外部联接

时间:2018-07-25 04:08:12

标签: sql

我试图连接3个表,但结果显示该列为空数据。请参考以下详细信息,并告知情况。赞赏!

我有什么桌子

1) EXPORT_SHIPMENT
SHPMNT_REF  UPDATE_USER    TYPE
1002            A           CS
1003            B           CA 

2) EXPORT_ONHAND
ONHAND_REF  UPDATE_USER     TYPE   SHPMNT_REF
 50001           A           CS      1002
 50002           B           CA      1003 

3) VW_EXPORT_EVENT_VCB
 FILE_NO    VCB_FA_CREATED_TIME
 50001        2018-07-26          
 50002        2018-07-25               

我所做的如下:

SELECT 
    ES.SHPMNT_REF,
    V.VCB_FA_CREATED_TIME
FROM 
    EXPORT_SHIPMENT ES 
LEFT OUTER JOIN
    (SELECT 
         OH.ONHAND_REF,
         VCB.FILE_NO,
         VCB.VCB_FA_CREATED_TIME
     FROM 
         EXPORT_ONHAND OH 
     INNER JOIN 
         VW_EXPORT_EVENT_VCB VCB ON OH.ONHAND_REF = VCB.FILE_NO) AS "V" ON ES.SHPMNT_REF = V.ONHAND_REF
WHERE
    ES.SHPMNT_REF <> '1001'

运行此代码后,“ V.VCB_FA_CREATED_TIME”列为空数据。

我想在结果中看到的是:

SHPMNT_REF    VCB_FA_CREATED_TIME
1002               2018-07-26
1003               2018-07-25

请协助。非常感谢!

2 个答案:

答案 0 :(得分:1)

You can try below Query

select ES.SHPMNT_REF,T.VCB_FA_CREATED_TIME from EXPORT_SHIPMENT ES 
  left join

  (
    select * from EXPORT_ONHAND EO
   inner join   
  VW_EXPORT_EVENT_VCB VW on EO.ONHAND_REF=VW.FILE_NO
    )
   as T 

  on ES.SHPMNT_REF=T.SHPMNT_REF

see below link

https://www.db-fiddle.com/f/i7mmehTxD8kh2h5BHeCdEi/1

答案 1 :(得分:1)

Try this.

 SELECT 
        ES.SHPMNT_REF,
        V.VCB_FA_CREATED_TIME
    FROM 
        EXPORT_SHIPMENT ES 
    LEFT OUTER JOIN
        (SELECT 
             OH.SHPMNT_REF,
             VCB.FILE_NO,
             VCB.VCB_FA_CREATED_TIME
         FROM 
             EXPORT_ONHAND OH 
         INNER JOIN 
             VW_EXPORT_EVENT_VCB VCB ON OH.ONHAND_REF = VCB.FILE_NO) AS V 
             ON ES.SHPMNT_REF = V.SHPMNT_REF
    WHERE
        ES.SHPMNT_REF <> '1001';