Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
there is two table in my mysql database.
one table called products has products_id and stock
another table customer basket products_id

for ex customer added 3 products

I want to do in store procedure

go to customer table get products_id one by one

get first product_id and compare products table with producs_id and I will look stock if stock is 0 delete customer basket table for this products_id

continue second,third...

so it can be loop???
Posted

1 solution

No - don't use a loop. Learn about JOIN[^]

An example:
SQL
DELETE FROM [customer basket]
WHERE products_id IN (
    SELECT cb.products_id FROM [customer basket] cb
    INNER JOIN [products] p ON cb.products_id = p.products_id
    WHERE p.stock = 0 )
 
Share this answer
 
v2
Comments
Member-2338430 12-Feb-15 8:38am    
I am trying...
Member-2338430 12-Feb-15 17:31pm    
giving error this =you cant specify target table [customer basket]for update in from clause
Member-2338430 12-Feb-15 19:53pm    
thanks I solved problem
Member-2338430 13-Feb-15 3:39am    
another question like this
there are two table same as;
pay table has product_id and how many product added so: number
product table same above product_id and stock
for ex X product stock=10 I paid or added 2 so product table stock must be update new value 8.
I tryed like above but I cant
update [products] set Stock=Stock-number
WHERE products_id IN (
SELECT cb.products_id,number FROM [pay] cb
INNER JOIN [products] p ON cb.products_id = p.products_id
WHERE cb.situation=1)
situation=1 means paid
CHill60 17-Feb-15 11:32am    
You can't select more than one column in the inner select - the one you used for the IN ... if you think about it what would sql compare the products_id to ... the cb.products_id or the number?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900