Update statement to deduct from other table
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开发者_如何学Go = ( product.ProductQuantity - sale.quantity)
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
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)
精彩评论