update statement using nested query

后端 未结 6 1814
梦谈多话
梦谈多话 2020-12-14 06:33

I have a table:

mytable:
    id
    userID
    logDate
    lastLogDate

For every row in that table, I want to update the \'lastLogDate\' co

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 07:21

    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
    

提交回复
热议问题