I have three tables t1
, t2
and t3
.
What I want is to update table t1
with t1.Quantity= sum(t2.quantity) - sum(t3.quantity)
where id= $_POST['id']
HOW TO WRITE QUERY FOR THIS.
I tried this one.. but its not working.
INSERT INTO Products
( `ProductID`, `ProductName`, `TotalQuantity`,
`TotalPrice`, `DateOfLastupdate` )
values
( '$ProductID', '$ProductName', '$Quantity',
'$TotalPrice', '$PurchaseDate' )
ON DUPLICATE KEY
UPDATE Products.TotalQuantity =
( select sum(Products_Purchased.Quantity) from Products_Purchased
where ProductID = '$ProductID' )
- ( select sum(Products_Sold.Quantity) from Products_Sold
where ProductID = '$ProductID' )