2

i have stock and cart, i want to make a query that shows each user a list of stock-cart

for example if i have 5 apples in stock and he have 3 apples in his cart, then when he search for more apples he should see only 2 (note: i group items by type)

basic table for stock

id,type,price,seller,...
1,apple,10,s1
2,apple,10,s2
3,orange,5,s1
3,apple,10,s1

assuming he selected 2 apples

for cart:

id,type,quantity
1,apple,2

so my query will simple select 2 random apples on checkout (yes this is how it should be according to my script).

So when i want to show items from stock again i do:

SELECT *,count(*) as counter FROM stock group by type

how i can get counter-(what he have in cart)?

4

1 に答える 1

3
select s.type, count(*) - (
  select count(*)
  from cart c
  where c.type=s.type
  # and c.userid = X
  ) as counter
from stock s
group by s.type
having counter > 0
于 2011-04-03T20:26:04.470 に答える