需要帮助从选择查询更新表

时间:2014-02-14 10:21:31

标签: mysql sql

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

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子句吗?

2 个答案:

答案 0 :(得分:0)

UPDATE matcontctsafter m SET m.InvoiceAmount = (SELECT 
SUM(v.UnitPrice)
FROM tblverification v 
LEFT JOIN tblorderdetailsafter d ON v.VMainID = d.MainID
WHERE m.OrderNo = d.OrderNo);

答案 1 :(得分:0)

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;