Reputation: 155
I have two tables: product
and sale
. How can I write a SQL statement to deduct sale item from a product?
I tried
UPDATE product,
sale
SET product = ( product.ProductQuantity - sale.quantity)
Upvotes: 0
Views: 931
Reputation: 54252
use this SQL statement
UPDATE product SET productquantity=(productquantity-(SELECT quantity FROM sale)) WHERE product_id={ some product id }
I added WHERE product_id={ some product id }
as you probably want to update specific product only
Upvotes: 2
Reputation: 9425
Depending on which value you are trying to update, you must specify the following:
UPDATE T1,T2 SET T1.Field = (T1.Field - T2.Field)
You were very close, however you must specify the field to update (where your product is)
Upvotes: 0