表:tab
id  name
3   张三
2   李四
1   王五用sql语句把最新添加的一条记录(在这里就是3这条)中的name修改为zhang3

解决方案 »

  1.   

    update tab
    set name='zhang3'
    where id=(select max(id) from tab)
      

  2.   

    update tab
    set name ='zhang3'
    where id<=3
      

  3.   

    update a set name='zhang3' from tab a where not exists (select 1 from tab b where a.id<b.id )
    --or
    update tab set name='zhang3' where id=(select max(id) from tab)
      

  4.   

    create table tab(id int,name nvarchar(10))
    insert into tab select 1,'王五'
    insert into tab select 2,'李四'
    insert into tab select 3,'张三'
    go
    update tab set name='zhang3' from tab a where not exists(select 1 from tab where id>a.id)
    select * from tab
    go
    drop table tab
    /*
    id          name
    ----------- ----------
    1           王五
    2           李四
    3           zhang3(3 行受影响)*/