在MySql中使用MAX()进行更新

时间:2011-03-14 10:41:54

标签: mysql sql

下面的代码很好但是......

如何在同一语句中使用带MAX()函数的Update?

类似于:UPDATE Msg SET myTime = NOW()WHERE user ='username'AND myTime = max(myTimp)

SELECT @myId:=(SELECT id FROM Msg
WHERE myTime=(SELECT MAX(myTime) FROM Msg
WHERE user='username'));

UPDATE Msq SET myTime=NOW() 
WHERE id=@myId;

1 个答案:

答案 0 :(得分:8)

  UPDATE Msg
     SET myTime=NOW()
   WHERE user='username'
ORDER BY myTime DESC
   LIMIT 1