How to sum price of products in cart per user
I'm trying to move some logics of my web shop application to database engine, so I figured that counting price of cart would be a good start. So I have a relation shown below with Cart_product table having foreign keys with Buyer and Product. The total price of cart for each user would 开发者_StackOverflowbe the price of each product in Cart_product multiplied by it's amount. How and with what can I achieve this ? Trigger, procedure, cursor ? Any help appreciated.
SELECT Buyer_ID, SUM(Amount * Product.ProductPrice)
FROM Cart_product
LEFT JOIN Product on Cart_product.Product_ID = Product.Product_ID
GROUP BY Buyer_ID
would return how much each user's bought. What you do with it from there is up to you.
For a specific user:
SELECT SUM(Amount * Product.ProductPrice)
FROM Cart_product
LEFT JOIN Product on Cart_product.Product_ID = Product.Product_ID
WHERE Buyer_ID = XXX
GROUP BY Buyer_ID
精彩评论