问题
I am trying to update a table filed taking value from another table by summing a field based on EmpID
.
In tblEmpInfo
EmpID
is primary key. I have saw few post on this site but couldn't adopt to my file. Here is my code till yet.
UPDATE tblEmpInfo AS c
INNER JOIN (SELECT EmpID, SUM(ProfidentFund) AS total FROM tblTransactions GROUP BY EmpID) AS x ON c.EmpID = x.EmpID
SET c.ProfidentFund = x.total;
Above code give me following warning and do not update value to tblEmpInfo
tblEmpInfo
screenshot.
tblTransactions
screenshot.
And my expected output.
回答1:
You can try the below -
UPDATE c
SET c.ProfidentFund = x.total
from tblEmpInfo AS c
INNER JOIN
(SELECT EmpID, SUM(ProfidentFund) AS total FROM tblTransactions GROUP BY EmpID) AS x
ON c.EmpID = x.EmpID
来源:https://stackoverflow.com/questions/62929358/update-table-field-by-summing-field-from-another-table