在ms访问中使用With语句的替代方法

时间:2018-01-14 15:12:40

标签: sql ms-access

我有这个SQL查询

WITH Stocktype AS (SELECT SUBSTRING([T2_LOOK].[KEY],4,6) AS StkType, T2_LOOK.F1 AS MasterStocktype
FROM T2_LOOK
WHERE Left([T2_LOOK].[KEY],3)='CAT'),
SELECT  *                           
FROM ((T2_BRA INNER JOIN T2_HEAD ON T2_BRA.REF = T2_HEAD.REF)  INNER JOIN Stocktype ON T2_HEAD.SUPPLIER = Stocktype .SuppCode)

我需要这个查询才能在ms访问中工作,我知道在ms访问中你不能使用语句WITH,有没有人知道一个替代方案或我如何让它在访问中工作?提前致谢

1 个答案:

答案 0 :(得分:1)

您可以将其替换为子查询:

SELECT  *                           
FROM (T2_BRA INNER JOIN
      T2_HEAD
      ON T2_BRA.REF = T2_HEAD.REF
     ) INNER JOIN
     (SELECT MID([T2_LOOK].[KEY], 4, 6) AS StkType,
             T2_LOOK.F1 AS MasterStocktype
      FROM T2_LOOK
      WHERE Left([T2_LOOK].[KEY], 3) = "CAT"
     ) as Stocktype
     ON T2_HEAD.SUPPLIER = Stocktype.SuppCode;

当然,这仍然无效(即使修复了MS Access语法),因为Stocktype没有名为SuppCode的列。但是,这是原始查询的问题,因此不清楚如何解决它。