开发者

Update statement to deduct from other table

开发者 https://www.devze.com 2023-02-15 17:37 出处:网络
I have two tables: product andsale.How can I write a SQL statement to deduct sale item from a product?

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消