update statement using nested query

后端 未结 6 1769
梦谈多话
梦谈多话 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:00

    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 ...
    
    0 讨论(0)
  • 2020-12-14 07:06

    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; 
    
    0 讨论(0)
  • 2020-12-14 07:18
    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;
    
    0 讨论(0)
  • 2020-12-14 07:20

    Following update statement should do what you are looking for

    update mytable mt set  lastLogDate  = (select max(logDate) from  mytable where userID = mt.userID)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-14 07:23

    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...;
    
    0 讨论(0)
提交回复
热议问题