I have a table dutyrecord which stores multiple user working hours (subtotal)
and a UserID (volunteerID). With the following selecting statement I am about to set a total hours for each UserID:
SELECT
dutyrecord.VolunteerID,
SEC_TO_TIME( SUM( TIME_TO_SEC( SubTotal ) ) ) AS total
FROM
volunteerinfo
INNER JOIN
dutyrecord ON
( volunteerinfo.VolunteerID = dutyrecord.VolunteerID )
GROUP BY
dutyrecord.VolunteerID
Could I combine an update statement with a select to update total hours to each user? I tried something like and some other method with no luck:
UPDATE volunteerinfo
SET
TotalHours =
(
SELECT dutyrecord.VolunteerID ,
SEC_TO_TIME( SUM( TIME_TO_SEC( SubTotal ) ) ) AS total
FROM
volunteerinfo
INNER JOIN
dutyrecord ON ( volunteerinfo.VolunteerID = dutyrecord.VolunteerID )
GROUP BY
dutyrecord.VolunteerID
)
WHERE
volunteerinfo.VolunteerID = dutyrecord.VolunteerID`
Can anyone give me a hand on this?
UPDATE
Sorry a few try and other examples I figure out the answer and a more clear way to ask my question.
To clear my question: Can I combine this process into one since I wants to make this process language independent? (I just carry 1 SQL statement in different perform.)
-
First, I select a table to give me the Sum of working hours for each person
SELECT VolunteerID, SEC_TO_TIME( SUM( TIME_TO_SEC( SubTotal ) ) ) AS total
FROM dutyrecord
GROUP BY VolunteerIDe.g. ID: 10001, 1:00:02 ID: 2001, 10:00:34 ….
-
After, I want to update the result (total) into another table according to their ID
update Volunteerinfo
set Volunteerinfo.totalhours = dutyrecord.total
where Volunteerinfo.VolunteerID = dutyrecord.VolunteerID
My answer to this is to first join both tables together then the update and select statement will work in one row. please also comment me if inner join or other ways will increase the performance of this query.