我遇到SQL更新查询问题:
UPDATE tblcurr
SET USD = 1
Order by Date1 Desc
limit 1
我希望查询只更新最新日期的记录。
我做错了什么?
答案 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)