2

我正在使用此查询来获取结果,

 SELECT * 
    FROM TRXN_REPORT CNT, 
    TBL_CUSTINFO CUSTADDINFO, 
    TBL_DEAL_EVT EVENT, 
    TBL_MISCADDR CIFMISC       
    WHERE CNT.INTERNAL_REF_NUM = EVENT.INTERNAL_REF_NUM 
    AND CNT.BRANCH = EVENT.BRANCH 
    AND CNT.QFXVERSION = EVENT.QFXVERSION 
    AND CNT.VALUEDATE = EVENT.VALUEDATE 
    AND CNT.LIQ_STATUS = 'L' 
    AND EVENT.EVENTCODE = 'LLIQ' -- Liquidation Event 
    AND CUSTADDINFO.CUSTOMER_NO = CNT.CUST_NUM 
    AND CIFMISC.CUSTOMER_NUMBER = CNT.CUST_NUM 
    AND CUSTADDINFO.REC_STATUS       ='O' 
    AND CIFMISC.REC_STATUS       = 'O' 
    AND MY_PBG_CUSTOMER = 'Y' 
    AND (BUYCCYCODE = 'USD' OR SELLCCYCODE ='USD') 

这里这个条件

和 CIFMISC.CUSTOMER_NUMBER = CNT.CUST_NUM

只获取那些同时存在于TBL_MISCADDRTRXN_REPORT表中的记录,现在作为要求的改变,我想执行左外连接,以便获取TBL_MISCADDR表中不存在但存在于TRXN_BATCHREPORT_WRK. 如何在保持所有 where 条件的同时执行此操作。

4

1 回答 1

0

您必须ONJOIN. 因此,您在 WHERE 子句中的当前条件应分为连接表的条件(这些条件已移至ON子句),其余用于选择行 - 这些应留在WHERE子句中。示例(不知道根据您的数据我是否正确,但您会明白的):

SELECT * 
FROM TRXN_REPORT CNT 
LEFT JOIN TBL_DEAL_EVT EVENT ON 
        CNT.INTERNAL_REF_NUM = EVENT.INTERNAL_REF_NUM 
    AND CNT.BRANCH = EVENT.BRANCH 
    AND CNT.QFXVERSION = EVENT.QFXVERSION 
    AND CNT.VALUEDATE = EVENT.VALUEDATE
LEFT JOIN SOME_OTHER_TABLE T ON
...
WHERE 
    AND CUSTADDINFO.REC_STATUS       ='O' 
    AND CIFMISC.REC_STATUS       = 'O' 
    AND MY_PBG_CUSTOMER = 'Y' 
    AND (BUYCCYCODE = 'USD' OR SELLCCYCODE ='USD') 
....

这样您就可以安全地使用LEFT JOIN,并且其他表中不存在记录的行也将被包括在内。

...获取TBL_MISCADDR表中不存在但存在于TRXN_BATCHREPORT_WRK.

这将使用TRXN_BATCHREPORT_WRK LEFT JOIN TBL_MISCADDR ON <condition>.

于 2013-07-01T09:43:18.060 回答