如果存在于其中一个表中,我无法显示我的行

时间:2014-09-22 11:34:34

标签: sql oracle oracle10g

有三个表,AGR_SALVAGUARDASAGR_MIT_FREC_TIPO包含数据,但AGR_MIT_FREC_ACT为空。我想在AGR_SALVAGUARDASAGR_MIT_FREC_TIPO中存在ID_SALVAGUARDA时提取AGR_MIT_FREC_ACT项,但不显示数据。我的问题是什么?

SELECT SAL.ID_SALVAGUARDA, SAL.DESCRIPCION, SAL.EFICACIA
FROM (  Select ID_SALVAGUARDA, ID_AMENAZA
        FROM AGR_MIT_FREC_TIPO
        WHERE ID_AMENAZA = 5043) MFT,
     (  Select ID_SALVAGUARDA, ID_AMENAZA
        FROM AGR_MIT_FREC_ACT
        WHERE ID_AMENAZA = 5043) MFA,  
      AGR_SALVAGUARDAS SAL
WHERE SAL.ID_SALVAGUARDA = MFT.ID_SALVAGUARDA
   OR SAL.ID_SALVAGUARDA = MFA.ID_SALVAGUARDA
GROUP BY SAL.ID_SALVAGUARDA, SAL.DESCRIPCION, SAL.EFICACIA

我正在使用Oracle 10g。提前谢谢

1 个答案:

答案 0 :(得分:2)

您需要使用left join。实际上,只需遵循一个简单的规则:永远不要在from子句中使用逗号。始终使用明确的join语法:

SELECT SAL.ID_SALVAGUARDA, SAL.DESCRIPCION, SAL.EFICACIA
FROM AGR_SALVAGUARDAS SAL LEFT JOIN
     AGR_MIT_FREC_TIPO MFT
     ON SAL.ID_SALVAGUARDA = MFT.ID_SALVAGUARDA AND MFT.ID_AMENAZA = 5043 LEFT JOIN
     AGR_MIT_FREC_ACT MFA
     ON SAL.ID_SALVAGUARDA = MFA.ID_SALVAGUARDA AND MFA.ID_AMENAZA = 5043   
WHERE MFT.ID_SALVAGUARDA IS NOT NULL OR
      MFS.ID_SALVAGUARDA IS NOT NULL
GROUP BY SAL.ID_SALVAGUARDA, SAL.DESCRIPCION, SAL.EFICACIA;