SQL查询左外连接具有特定记录

时间:2015-11-03 03:15:21

标签: sql oracle

我有这样的查询

SELECT P9_IR259_FI, FX_IR259_SYS_PROD_TYP, FX_IR259_SLS_POS, FX_IR259_SLS_CHNL, PX_IR259_OFC_CDE, FX_IR259_SPR_OFC_CDE, FX_IR259_NAME, SUPERNAME, SUPERCDE, SUPERPOS 
FROM IR259 K 
     LEFT OUTER JOIN
         (SELECT FX_IR259_NAME AS SUPERNAME ,FX_IR259_SLS_POS AS SUPERPOS, PX_IR259_OFC_CDE AS SUPERCDE 
         FROM IR259 M 
         WHERE P9_IR259_FI = 75214 AND FX_IR259_SLS_POS = 2 AND FX_IR259_NAME LIKE 'ME%'
         ) ON K.FX_IR259_SPR_OFC_CDE = SUPERCDE 
 WHERE 
    FX_IR259_SLS_POS = '1' 
    AND FX_IR259_SLS_CHNL = 'BRO' 
    AND F9_IR259_CRE_TMS > 00000000000000000 
ORDER BY FX_IR259_SLS_POS, FX_IR259_SPR_OFC_CDE, FX_IR259_SLS_CHNL

但结果似乎不正确意味着,我想显示SUPERNAME喜欢' ME%'仅

enter image description here

结果显示SUPERNAME有类似的' ME%'但其他记录也表明了。

感谢您的建议

3 个答案:

答案 0 :(得分:1)

您需要在where子句中添加另一个过滤器。

WHERE 
    FX_IR259_SLS_POS = '1' 
    AND FX_IR259_SLS_CHNL = 'BRO' 
    AND F9_IR259_CRE_TMS > 00000000000000000 
    AND SUPERNAME LIKE 'ME%'

答案 1 :(得分:1)

您只需要在where子句

中添加另一个条件
SELECT P9_IR259_FI, FX_IR259_SYS_PROD_TYP, FX_IR259_SLS_POS, FX_IR259_SLS_CHNL, PX_IR259_OFC_CDE, FX_IR259_SPR_OFC_CDE, FX_IR259_NAME, SUPERNAME, SUPERCDE, SUPERPOS 
FROM IR259 K 
    LEFT OUTER JOIN
         (SELECT FX_IR259_NAME AS SUPERNAME ,FX_IR259_SLS_POS AS SUPERPOS, PX_IR259_OFC_CDE AS SUPERCDE 
          FROM IR259 M 
         WHERE P9_IR259_FI = 75214 AND FX_IR259_SLS_POS = 2 AND FX_IR259_NAME LIKE 'ME%'
         ) ON K.FX_IR259_SPR_OFC_CDE = SUPERCDE 
 WHERE 
    FX_IR259_SLS_POS = '1' 
    AND FX_IR259_SLS_CHNL = 'BRO' 
    AND F9_IR259_CRE_TMS > 00000000000000000 
    AND SUPERNAME LIKE 'ME%'
ORDER BY FX_IR259_SLS_POS, FX_IR259_SPR_OFC_CDE, FX_IR259_SLS_CHNL

答案 2 :(得分:0)

SELECT P9_IR259_FI, FX_IR259_SYS_PROD_TYP, FX_IR259_SLS_POS, FX_IR259_SLS_CHNL, PX_IR259_OFC_CDE, FX_IR259_SPR_OFC_CDE, FX_IR259_NAME, SUPERNAME, SUPERCDE, SUPERPOS 
FROM 
(select * from IR259 where SUPERNAME like 'ME%') K 
     LEFT OUTER JOIN
         (SELECT FX_IR259_NAME AS SUPERNAME ,FX_IR259_SLS_POS AS SUPERPOS, PX_IR259_OFC_CDE AS SUPERCDE 
         FROM IR259 M 
         WHERE P9_IR259_FI = 75214 AND FX_IR259_SLS_POS = 2 AND FX_IR259_NAME LIKE 'ME%'
         ) ON K.FX_IR259_SPR_OFC_CDE = SUPERCDE 
 WHERE 
    FX_IR259_SLS_POS = '1' 
    AND FX_IR259_SLS_CHNL = 'BRO' 
    AND F9_IR259_CRE_TMS > 00000000000000000 
ORDER BY FX_IR259_SLS_POS, FX_IR259_SPR_OFC_CDE, FX_IR259_SLS_CHNL