Reputation: 377
I'm really struggling with this one. SO I have 2 tables:
Here's a screenshot of structure for both tables:
I need to get the SUM for all 3 products WHERE pending_cart_id = 18
.
SELECT SUM(price) as TotalCartPrice FROM products WHERE id = '274'
How can I write it so it sums all 3 id's (274+251+49)?
Upvotes: 0
Views: 53
Reputation: 10184
Would something like this not work?
Select sum(b.price*a.quantity)
from pending_cart_items a
join products b
on a.product_id=b.id
where a.pending_cart_id =18
Edit: Just realized I'd omitted the quantity from the cart computation :)
Upvotes: 1
Reputation: 3606
You'll need to join the two tables:-
select
sum(p.price)
from Products p
inner join PendingCartItems pci on p.id= pci.product_id
where pci.pending_cart_id = 18
Upvotes: 0
Reputation: 223
If the model is relational, you can try this
SELECT
SUM(price) as TotalCartPrice
FROM products
INNER JOIN PendingCartItems ON products.id = PendingCartItems.product_id
WHERE PendingCartItems.pending_cart_id = 18
GROUP BY PendingCartItems.pending_cart_id
Upvotes: 0