使用同一查询中的查询结果

时间:2015-03-31 22:34:51

标签: mysql sql sql-server ms-access join

我有一个相当大的SQL语句,我正在MS Access 2010中使用。这里是:

SELECT 
W.ID AS wid,
W.wpt_ty AS ty,
W.wpt_num AS num,
W.wpt_nxt AS nxt,
W.latdeg AS lat,
W.londeg AS lon,
W.alt AS alt,
W.mission_id AS mid,
W.ctg1 AS ctg1,
W.ctg2 AS ctg2,
W.ctg3 AS ctg3,
W.ctg4 AS ctg4,
W.wpt_index AS indx,
W.vel AS vel,
W.tu AS tu,
R.route_num AS rnum,
R.AC_num AS ac,
R.route_type AS rtype,
R.LastUpdatedOn AS d8,
R.LastUpdatedBy AS auth,
R.flight_wpt_count AS wfcount,
M.mission_name AS msnName,
V.Description AS vstatus,
R.disallowed_reason_id AS did,
CW.wpt_num AS c1num,
CR.matching_route_id AS c1mrid,
CW.wpt_index AS c1indx,
CRU.runway_name AS c1rnwy,
CR.route_num AS c1rnum
FROM Validation AS V 
(RIGHT JOIN Runways AS CRU 
 INNER JOIN (Routes CR 
 INNER JOIN Waypoints CW ON CR.ID = CW.route_id) 
 ON Runways.ID = Routes.runway_id
INNER JOIN ((Missions as M 
INNER JOIN Routes AS R ON M.ID = R.mission_id) 
INNER JOIN Waypoints AS W ON (R.ID = W.route_id) 
AND (M.ID = W.mission_id)) ON 
V.ID = R.validated 
WHERE (((R.matching_route_id)=307543) AND ((R.validated) <> 0 ))
AND (((CW.mission_id)=mid) AND ((CW.wpt_num) = (ctg1))))

如果查看底部,您可以看到我在右连接上引用值ctg1mid,而内部连接引用其他文字值。最后,我想对ctg2ctg3ctg4

执行相同操作

现在我将这些作为两个单独的查询运行,但发现它太慢了。如果我可以加入组合查询(有点像我在这里展示的那样),它可以大大加快速度。但我不知道如何:

  • 在内部/左侧连接中使用所述查询中的选择值,并将它们推送到右连接所需的值。
  • 我可能错误地使用了连接,但我认为它们只需要在不同的支点上组合来自可能相同表的数据。
  • 如何使用MS Access GUI帮助编写这样的查询。
  • 我知道这是针对MS Access的,但我正在为MySQL标记以防万一有类似的查询可以移植到MS Access?

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用UNION?

它允许您执行此查询(作为两个查询,您提到的可能性),并加入输出的结果。

请注意,它会在结果集中耗尽(仅显示其中一个)重复项。

我还建议您阅读不同类型的联接,以便您自己获益,请参阅以下答案:

MYSQL Joins