Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please look this article
how can I compare two table and delete some data in mysql procedures?[^]

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 tried like above but I cant
SQL
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
Posted
v2

1 solution

Try this code:

#Pay has 3 Columns (ProductName, Added, Situation)
#Product has 2 columns (ProductName, Stock)

SQL
UPDATE x
SET x.Stock = x.Stock - y.Added
FROM #Product x
INNER JOIN #Pay y ON x.ProductName = y.ProductName
WHERE y.Situation = 1
 
Share this answer
 

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