表名: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 多条记录都更新了,请问怎么才能只更新最后一条记录
     

解决方案 »

  1.   


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

  2.   

    update abc set outdate=getdate() where id=(select max(id) from abc)
      

  3.   

    自己搞出来了
    update abc set outdate=getdate() where id=(select top 1 id from abc where username='admin' order by id desc)
      

  4.   

    update abc set outdate=getdate() where id=(select max(id) from abc)
      

  5.   


    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 @abc1 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
      

  6.   

    update   abc  
    set   outdate=getdate()   
    where   id=(select max(id) from abc)username='admin' 可以不写。另外主要日期格式