请问怎么用update更新数据表里的最后一条记录?

mailto520 2008-11-01 11:09:55
表名:abc

id username outdate
1 admin 2008-08-10
2 admin 2008-08-12
3 admin 2008-10-15

现在我要将最后一条记录(id=3)的记录的outdate更新为今天,现在我不知道ID。
如果我用 update abc set outdate=getdate() where username=admin 多条记录都更新了,请问怎么才能只更新最后一条记录
...全文
1797 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
njtimenowht 2008-11-01
  • 打赏
  • 举报
回复
update abc
set outdate=getdate()
where id=(select max(id) from abc)

username='admin' 可以不写。

另外主要日期格式
ChinaJiaBing 2008-11-01
  • 打赏
  • 举报
回复

declare @abc table(id int,username varchar(10),outdate datetime)
insert into @abc select 1,'admin','2008-08-10'
union all select 2,'admin','2008-08-12'
union all select 3,'admin','2008-08-15'
update a set a.outdate=getdate() from @abc a join (select top 1 * from @abc order by id desc)b
on a.id=b.id
select * from @abc

1 admin 2008-08-10 00:00:00.000
2 admin 2008-08-12 00:00:00.000
3 admin 2008-11-01 11:28:34.920
Andy-W 2008-11-01
  • 打赏
  • 举报
回复
update abc set outdate=getdate() where id=(select max(id) from abc)
mailto520 2008-11-01
  • 打赏
  • 举报
回复
自己搞出来了
update abc set outdate=getdate() where id=(select top 1 id from abc where username='admin' order by id desc)
yiyi_wx 2008-11-01
  • 打赏
  • 举报
回复
update abc set outdate=getdate() where id=(select max(id) from abc)
hsie168518 2008-11-01
  • 打赏
  • 举报
回复

update   abc   set   outdate=getdate()   where   username=admin   and id=3

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧