I have a table:
mytable:
id
userID
logDate
lastLogDate
For every row in that table, I want to update the \'lastLogDate\' co
I don’t know if I understood you correctly. Otherwise be a bit more specific, but from what I get, you should do something along the lines of:
UPDATE `mytable`
SET lastLogDate = (SELECT statement goes here)
WHERE ...
You can do this:
UPDATE t
SET t.logDate = t2.LatestDate
FROM YourTable t
INNER JOIN
(
SELECT userID, MAX(LogDate) LatestDate
FROM YourTable
GROUP BY userID
) t2 ON t.userID = t2.userID;
UPDATE mytable mT,
(SELECT userid,
MAX(logDate) AS maxDateForUser
FROM mytable
GROUP BY userId) t
SET mT.lastLogDate = t.maxDateForUser
WHERE mT.userid = t.userid;
Following update statement should do what you are looking for
update mytable mt set lastLogDate = (select max(logDate) from mytable where userID = mt.userID)
Something like this?
UPDATE mytable SET lastLogDate = t.maxDateForUser
FROM
(
SELECT userid, MAX(logDate) as maxDateForUser
FROM mytable
GROUP BY userId
) t
WHERE mytable.userid = t.userid
you can simply write a nested query like this
Update mytable a
set
a.lastLogDate = (select max(logDate) from mytable b
where a.id=b.id)
Where...;