[英]Update table using other two tables
我有三個表t1
, t2
和t3
。
我想要的是用t1.Quantity= sum(t2.quantity) - sum(t3.quantity) where id= $_POST['id']
更新表t1
, t1.Quantity= sum(t2.quantity) - sum(t3.quantity) where id= $_POST['id']
如何為此寫查詢。
我嘗試了這個..但是沒有用。
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' )
可能有幫助
更新table1,table2 SET table1.column1 =(SELECT SUM((來自table3的SELECT常量)+(SELECT table2.sum_number *** WHERE table2.table2_id1 = table1.id)))WHERE table1.id = table2.table2_id1;
UPDATE table1 SET column1 =(SUM(table2 {&table3} WHERE table2_id1 = id1)WHERE id1 = table2_id1
嘗試這個
update products t1,
(select productid,sum(Products_Purchased.Quantity) as x from Products_Purchased
group by productid having ProductID = '$ProductID' ) t2,
(select productid,sum(Products_Sold.Quantity) as y from Products_Sold
group by productid having ProductID = '$ProductID' ) t3
set TotalQuantity=t2.x-t3.y where t1.ProductID = '$ProductID'
and t1.productid=t2.productid and t1.productid=t3.productid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.