tee
tee

Reputation: 155

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 = ( product.ProductQuantity - sale.quantity)

Upvotes: 0

Views: 931

Answers (2)

Raptor
Raptor

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

Simon
Simon

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

Related Questions