有三个表,AGR_SALVAGUARDAS
和AGR_MIT_FREC_TIPO
包含数据,但AGR_MIT_FREC_ACT
为空。我想在AGR_SALVAGUARDAS
或AGR_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。提前谢谢
答案 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;