我有以下代码生成订单号和值的列表...

SELECT
 d.`OrderNo`,
 SUM(v.`UnitPrice`)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`;


我需要更新一个名为matcontctsafter的表,该表具有一个OrderNo字段,并且当前空白的InvoiceAmount列需要相对的SUM(v.UnitPrice)

有人可以帮我构造UPDATE子句吗?

最佳答案

UPDATE matcontctsafter m
INNER JOIN (
SELECT
 d.`OrderNo`,
 SUM(v.`UnitPrice`) InvoiceAmount
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`
) sq ON m.OrderNo = sq.OrderNo
SET m.InvoiceAmount = sq.InvoiceAmount;

10-04 10:50