0

我用 Microsoft Access 编写了这个查询:

SELECT p.artnbr                            AS [Number], 
   p.name                              AS Name, 
   s.sizename                          AS Sizes, 
   s.sizeindex                         AS SizeIndex, 
   s.oid                               AS SizeId, 
   l.name                              AS LocationName, 
   (SELECT od.quantity 
    FROM   orderdetails od 
    WHERE  od.ORDER = (SELECT o.oid 
                       FROM   [order] o 
                       WHERE  o.active = -1 
                              AND o.location = l.oid) 
           AND od.productsize = s.oid) AS Quantity 
FROM   [size] AS s 
   INNER JOIN (product AS p 
               INNER JOIN (favorite AS f 
                           INNER JOIN location AS l 
                                   ON f.customer = l.customer) 
                       ON p.oid = f.product) 
           ON p.oid = s.product 
WHERE  f.customer = @customer 

下面的链接显示了我使用的表格及其关系。 https://dl.dropbox.com/u/18377860/QueryTables.png

此查询返回正确的结果,但如您所见,我正在使用子查询来获取数量。我不知道如何使用连接语句而不是那个长子查询来重写这个查询。任何帮助,将不胜感激。
问候

编辑:为了更清楚,我的查询需要获取(产品、尺寸、位置)的所有唯一组合,无论它们是否有 [Order] 并显示订购数量。如果特定组合的订单不存在,则数量应为空。

EDIT2:我设法构建了查询,它看起来像这样:

SELECT p.ArtNbr                    AS [Number], 
   p.Name                          AS Name, 
   s.SizeName                      AS Sizes, 
   s.SizeIndex                     AS SizeIndex, 
   s.Oid                           AS SizeId, 
   l.Name                          AS LocationName, 
   so.qty                          AS Quantity
FROM ([Size] AS s 
         INNER JOIN (Product AS p 
               INNER JOIN (Favorite AS f 
                           INNER JOIN Location AS l 
                                   ON f.Customer = l.Customer) ON p.OID = f.Product) 
                       ON p.OID = s.Product) 
      LEFT JOIN 
          (SELECT od.ProductSize       AS PS, 
                  od.Quantity          AS qty, 
                  o.Location as Location 
           FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID 
           WHERE o.Active = -1)  AS so ON so.PS = s.OID
WHERE f.Customer = @customer AND (l.OID = so.Location OR so.Location is null)

我确实像马特说的那样,我将子查询移到 FROM 部分,我在派生表的选择查询中包含 ProductSize 和 Location,然后将结果与“Size”表链接,最后我在 where 部分添加了一个条件以消除每个位置的相同记录的重复。

4

1 回答 1

1

我设法构建了查询,它看起来像这样:

SELECT p.ArtNbr                    AS [Number], 
   p.Name                          AS Name, 
   s.SizeName                      AS Sizes, 
   s.SizeIndex                     AS SizeIndex, 
   s.Oid                           AS SizeId, 
   l.Name                          AS LocationName, 
   so.qty                          AS Quantity
FROM ([Size] AS s 
         INNER JOIN (Product AS p 
               INNER JOIN (Favorite AS f 
                           INNER JOIN Location AS l 
                                   ON f.Customer = l.Customer) ON p.OID = f.Product) 
                       ON p.OID = s.Product) 
      LEFT JOIN 
          (SELECT od.ProductSize       AS PS, 
                  od.Quantity          AS qty, 
                  o.Location as Location 
           FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID 
           WHERE o.Active = -1)  AS so ON so.PS = s.OID
WHERE f.Customer = @customer AND (l.OID = so.Location OR so.Location is null)

我确实像马特说的那样,我将子查询移到 FROM 部分,我在派生表的选择查询中包含 ProductSize 和 Location,然后将结果与“Size”表链接,最后我在 where 部分添加了一个条件以消除每个位置的相同记录的重复。

于 2012-08-30T09:02:11.790 回答