在4 Join Join Query中插入子查询

时间:2018-04-20 09:53:05

标签: sql ms-access

我在我的数据库中有以下查询,它可以正常工作并返回我需要的数据:

PARAMETERS [queryDate] DateTime = Date();
SELECT [Supplier Details].[Supplier Name] AS [Supplier Name], [Coster Suppliers].[Supplier Name] AS [Coster Supplier Name], [Product Details].[Product ID] AS [Product Details_Product ID], [Product Details].[Product Name], [Product Details].[Product Order Sheet Sequence], [Product Details].[Supplier ID], [Product Details].[Product Specification], [Product Details].[Supplier Order Code], [Product Details].[Hide Product], [Product Details].[Coster Supplier ID], [Product Details].[Hide Product from Stock], [Product Details].[Replacement Product ID], [Product Details].[Replacement Product Conversion Factor], [Product Details].[Product PLU], [Known Product Prices for Date Query].[Price Effective from Date], [Known Product Prices for Date Query].[Pack Count], [Known Product Prices for Date Query].[Pack Size], [Known Product Prices for Date Query].[Product Price], [Product Details].[Unit of Measure]
FROM ([Coster Suppliers] RIGHT JOIN ([Supplier Details] INNER JOIN [Product Details] ON [Supplier Details].[Supplier ID] = [Product Details].[Supplier ID]) ON [Coster Suppliers].[Supplier ID] = [Product Details].[Coster Supplier ID]) INNER JOIN [Known Product Prices for Date Query] ON [Product Details].[Product ID] = [Known Product Prices for Date Query].[Product ID];

我的问题是该字段[Coster供应商]。[供应商名称] AS [Coster供应商名称]来自另一个查询,我需要作为select语句插入此查询但我不知道应该去哪里。我在插入它时遇到了重大问题,我确信这是一个简单的语法问题。要插入而不是引用它的查询如下:

SELECT [Coster Supplier Details].[Supplier ID], [Coster Supplier Details].[Supplier Type ID], [Coster Supplier Details].[Supplier Name], [Coster Supplier Details].[Supplier Purchase Tracker Sequence], [Coster Supplier Details].[Supplier Rep], [Coster Supplier Details].[Supplier Account Number], [Coster Supplier Details].[Supplier Sales Hotline], [Coster Supplier Details].[Supplier Sales Fax], [Coster Supplier Details].[Produce new week's order summary based on previous week], [Coster Supplier Details].[Supplier Phone], [Coster Supplier Details].[Supplier Fax], [Coster Supplier Details].[Supplier E-Mail], [Coster Supplier Details].[Supplier Web], [Coster Supplier Details].[Supplier Address 1], [Coster Supplier Details].[Supplier Address 2], [Coster Supplier Details].[Supplier Address 3], [Coster Supplier Details].[Supplier City], [Coster Supplier Details].[Supplier County], [Coster Supplier Details].[Supplier Postcode], [Coster Supplier Details].[Supplier Country], [Coster Supplier Details].[Minimum Order Value], [Coster Supplier Details].[Standard Delivery Cost], [Coster Supplier Details].[Delivery Cost if order value < minimum], [Coster Supplier Details].[Supplier Notes], [Coster Supplier Details].[Hide Supplier], [Coster Supplier Details].[Coster Supplier], [Coster Supplier Details].[Supplier Integration 1], [Coster Supplier Details].[Supplier Integration 2], [Coster Supplier Details].[Accounts Ref]
FROM [Supplier Details] AS [Coster Supplier Details]
WHERE [Coster Supplier Details].[Coster Supplier]=True;

我知道引用查询会很简单,但我不能因为各种沉闷和无聊的原因而这样做。

为了进一步复杂化,它是访问2003. MS Office的最后一个伟大版本。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您应该能够将另一个查询添加为子查询并加入原始查询,如下所示:

`SELECT [cds].[Supplier ID]
      , [cds].[Supplier Type ID]
      , [sup].[Supplier Name]
      , [cds].[Supplier Purchase Tracker Sequence]
      , [cds].[Supplier Rep]
      , [cds].[Supplier Account Number]
      , [cds].[Supplier Sales Hotline]
      , [cds].[Supplier Sales Fax]
      , [cds].[Produce new week's order summary based on previous week]
      , [cds].[Supplier Phone]
      , [cds].[Supplier Fax]
      , [cds].[Supplier E-Mail]
      , [cds].[Supplier Web]
      , [cds].[Supplier Address 1]
      , [cds].[Supplier Address 2]
      , [cds].[Supplier Address 3]
      , [cds].[Supplier City]
      , [cds].[Supplier County]
      , [cds].[Supplier Postcode]
      , [cds].[Supplier Country]
      , [cds].[Minimum Order Value]
      , [cds].[Standard Delivery Cost]
      , [cds].[Delivery Cost if order value < minimum]
      , [cds].[Supplier Notes]
      , [cds].[Hide Supplier]
      , [cds].[Coster Supplier]
      , [cds].[Supplier Integration 1]
      , [cds].[Supplier Integration 2]
      , [cds].[Accounts Ref]
FROM [Supplier Details] AS [cds]
JOIN
(
    SELECT [Supplier ID]
         , [Supplier Name] 
    FROM [Other Query]
) [sup]
    ON [cds].[Supplier ID] = [sup].[Supplier ID]

WHERE [cds].[Coster Supplier]=True;`