SQL - 子查询中的多个字段输入主查询

时间:2013-09-18 00:10:03

标签: sql nested-queries

我有2个离散查询,这两个查询都会按照我的预期返回结果。 第一个查询返回数据库中的ID值对。 样本对是:[1,2],[3,6],[10,15]

通过迭代插入行程对进行第二次查询,按预期检索数据列表。

9979和240值是查询目的的常量。

有关如何在第二个查询中嵌套第一个查询的任何建议,以便我一次获得所有结果,按照从查询1中检索到的HID和SID进行分组? (6000次离散迭代不是一个非常整洁的解决方案)

SELECT     TH1.THR_ID AS HID, SHEAD.THR_ID AS SID
        FROM         RTDEV.TRIP_HEADERS SHEAD INNER JOIN
        RTDEV.TRIP_LINKS TLINK ON SHEAD.TME_ID = TLINK.TME_ID AND SHEAD.THR_ID =
        TLINK.PRIOR_SBSQUNT_THR_ID INNER JOIN
        RTDEV.TRIP_HEADERS TH1 ON TLINK.TME_ID = TH1.TME_ID AND TLINK.THR_ID = TH1.THR_ID
        WHERE     (SHEAD.TME_ID = 9979) AND (TLINK.TME_ID = 9979) AND (TH1.TME_ID = 9979)         AND
        (TLINK.LINK_TYPE = 'S')
        ORDER BY SHEAD.TRIP_NAME

SELECT     ‘1’ AS HID, ‘2’ AS SID, RTDEV.TRIP_HEADERS.TRIP_NAME,
     RTDEV.TRIP_HEADERS.DOP_REF,RTDEV.TRIP_BODIES.DEPARTURE_TIME,RTDEV.TNODED.STRLONGNAME
        FROM         RTDEV.TRIP_BODIES INNER JOIN
        RTDEV.TNODED ON RTDEV.TRIP_BODIES.MASTER_NODE = RTDEV.TNODED.NID INNER JOIN
        RTDEV.TRIP_HEADERS ON RTDEV.TRIP_BODIES.TME_ID = RTDEV.TRIP_HEADERS.TME_ID AND 
        RTDEV.TRIP_BODIES.THR_ID = RTDEV.TRIP_HEADERS.THR_ID
        WHERE (RTDEV.TNODED.NPROJECTGEOGRAPHYID = 240) AND (RTDEV.TRIP_BODIES.TME_ID =
        9979) AND (RTDEV.TNODED.STRLONGNAME IS NOT NULL) 
        AND (RTDEV.TRIP_BODIES.ARRIVAL_TIME <> RTDEV.TRIP_BODIES.DEPARTURE_TIME) AND
        (RTDEV.TRIP_HEADERS.TME_ID = 9979) AND 
        (RTDEV.TRIP_HEADERS.THR_ID IN (1, 2))
        ORDER BY RTDEV.TRIP_BODIES.DEPARTURE_TIME

0 个答案:

没有答案