ShoppingCart的复杂SQL查询

时间:2010-02-16 17:00:20

标签: sql database database-design select

我一直在试图找出如何查询购物车数据库,以查找包含尚未分配UPS跟踪标签的有形商品(物品可以下载,因此未发货)的所有订单。我无法做到。

所涉及的表格如下:

// dbo.Inventory - details about the individual product being sold
- ProductID int primary - Name nvarchar - IsDownloadable bit -
| 5                     | Awesome Shirt  | 0                  |
| 7                     | An Audio Track | 1                  |

// dbo.ShoppingCart --("ShopID" groups the items in the cart)
- CartID int primary - ProductID int - ShopID char (guid) - Quantity int -
| 2                  | 5             | e854a982c9264a72   | 4            |
| 3                  | 7             | e854a982c9264a72   | 1            |

// dbo.Orders - Order information (shipping address, etc)
- OrderID int primary - ShopID char(x)    - BillingInfoColumns -
| 13                  | e854a982c9264a72  | Name,Address,etc   |

// dbo.Tracking - Shipments' (note: a shipment can contain several items) tracking numbers
- TrackingID int primary - OrderID int - TrackingNumber char(x) -
| 5                      | 13          | Ze5Whatever...         |

// dbo.ShippedItems - Maps a ShoppingCart's shipped items to tracking numbers
- ShippingID int primary - TrackingID int - CartID int - QuantityInShipment int
| 6                      | 5              | 2          | 3                      |

希望上面提供了DB设计方式的合理近似值。

所以,澄清一下我认为我需要的东西:

  • 选择所有尚未包含所有有形物品的订单ID。

  • 非有形物品是IsDownloadable = 1

  • 必须考虑ShoppingCart.Quantity列。如果我们订购4件T恤,我们可以将它们放在一个盒子里(带有一个UPS跟踪标签)。然后,我们可以每箱2个。或者我们可以将一条牛仔裤和一件衬衫放在同一个盒子里(再次,带一个跟踪标签)......等等。

我一直在用无尽的JOIN编写垃圾,并且嵌套WHERE NOT IN(SELECT * FROM)s无济于事。可悲的是,我似乎无法绕过它......我还在等待我的尤里卡时刻。

我对SQL和数据库设计比较陌生,所以任何信息或(建设性的)批评都会受到高度赞赏。如果您认为这会有所帮助,请随意在数据库本身的设计中挖洞。 : - )


// I wish I could run this on my brain right now...
// (Neurons, apparently, are "excitable")
UPDATE Brain SET Neuron = 'Excited' WHERE Cortex = 'SQL'

更新

感谢Benoit Vidis,我想出了这些。这是我在真实表/数据上使用的实际查询:

    SELECT 
        d.OrderID
    FROM
        Person.ShoppingCart c
    JOIN
        Inventory.Item i
    ON
        i.ItemID = c.ItemID
    JOIN
        Orders.Details d
    ON
        d.ShopID = c.ShopID
    LEFT JOIN
        Orders.Shipping s
    ON
        d.OrderID = s.OrderID
    LEFT JOIN
        Orders.ShippedItems si
    ON
        s.ShippingID = si.ShippingID
    WHERE
        i.DownloadableMedia = 0 AND
        d.Billed = 1 AND
        d.Ordered = 1
    GROUP BY
        d.OrderID
    HAVING
        SUM(c.Quantity) > CASE WHEN SUM(si.Quantity) IS NULL THEN 0 ELSE SUM(si.Quantity) END

2 个答案:

答案 0 :(得分:1)

您可以使用HAVING子句来完成它。在MySQL中,它会提供类似的东西:

SELECT 
  c.OrderID,
  SUM(c.Quantity) AS tangible_products_number,
  SUM(s.QuantityInShipment) as shipped_items_number
FROM
( 
  Inventory i,
  ShoppingCart c
)
LEFT JOIN
  ShippedItems s
ON
  c.OrderID = s.OrderID
WHERE
  i.ProductID = c.ItemID AND
  i.IsDOwnloadable = 0 AND
  c.OrderID = t.OrderID AND
  s.CartID = c.ID
GROUP BY
  c.OrderID
HAVING
  SUM(c.Quantity) > SUM(s.QuantityInShipment)

语法分组可能需要适用于SQL-Server

答案 1 :(得分:0)

你能查询dbo.Tracking里的TrackingNumber是否为Null?这会给你所需的信息吗?