将3个查询合并为一个子查询

时间:2018-10-08 09:09:20

标签: oracle oracle11g oracle-sqldeveloper

是否可以编写单个查询以从3个表中获取数据? 以下是我正在运行的3个查询,用于获取数据,但需要一个查询。

我尝试编写以下查询,但是我获取的数据错误

(SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALCOM_DBF  WHERE  M_GF_IND = 'Y' ) A
(SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALIRD_DBF  WHERE  M_GF_IND = 'Y' ) B
(SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALSCF_DBF  WHERE  M_GF_IND = 'Y' ) C

我尝试编写以下查询,但是我获取的数据错误

SELECT DD.M_NB, DD.M_GF_IND, DD.M_HEDGE_LNK,
       FF.M_NB, FF.M_GF_IND, FF.M_HEDGE_LNK 
FROM TABLE#DATA#DEALCOM_DBF DD,
     TABLE#DATA#DEALIRD_DBF FF  
WHERE  DD.M_GF_IND = 'Y' and FF.M_GF_IND = 'Y'   

很抱歉,如果这是一个不好的问题,我是SQL新手,因此无法弄清楚。如果有其他选择,将会更新。

1 个答案:

答案 0 :(得分:3)

您要寻找UNIONALL)吗?

SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALCOM_DBF  WHERE  M_GF_IND = 'Y' 
UNION ALL
SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALIRD_DBF  WHERE  M_GF_IND = 'Y' 
UNION ALL
SELECT M_NB,M_GF_IND,M_HEDGE_LNK  FROM TABLE#DATA#DEALSCF_DBF  WHERE  M_GF_IND = 'Y';