SQL Update最新记录

时间:2015-10-10 19:20:38

标签: sql sql-server

我遇到SQL更新查询问题:

UPDATE tblcurr
SET USD = 1 
Order by Date1 Desc 
limit 1

我希望查询只更新最新日期的记录。

我做错了什么?

3 个答案:

答案 0 :(得分:2)

<强> LiveDemo

UPDATE tblcurr 
SET USD = 1 
WHERE Date1 = (SELECT MAX(Date1) FROM tblcurr)

如果它们具有相同的最大Date1

,它可能会更新多个记录

答案 1 :(得分:1)

因为@ lad2025已经提到了It may update multiple records if they have the same max Date1

如果您只想更新一条记录,则可以尝试使用ROW_NUMBER ()

;with cte as
(select *, ROW_NUMBER() over (order by Date1 desc)as rn
from tblcurr)
update cte set USD=1 where rn=1

答案 2 :(得分:0)

尝试这个

UPDATE tblcurr
SET USD = 1 
where Date1 = MAX(Date1)