更新最新一条数据的SQL语句写法怎么写,将C字段下的null(空值)更成‘已更新’或者别的东西请随便。
只更新最新一条
数据如下:
CITY  DATE                          C 
武汉  2009-9-21 13:45:00          32 
长沙  2009-9-21 13:46:00          null 
长沙  2009-9-21 13:47:00          31 
长沙  2009-9-21 13:48:00          null 
上海  2009-9-21 13:49:00          29 
上海  2009-9-21 13:50:00          38 
武汉  2009-9-21 13:51:00          31 
上海  2009-9-21 13:52:00          null 
武汉  2009-9-21 13:53:00          31 我要结果是:
CITY  DATE                          C 
武汉  2009-9-21 13:45:00          32 
长沙  2009-9-21 13:46:00          null 
长沙  2009-9-21 13:47:00          31 
长沙  2009-9-21 13:48:00          已更新 
上海  2009-9-21 13:49:00          29 
上海  2009-9-21 13:50:00          38 
武汉  2009-9-21 13:51:00          31 
上海  2009-9-21 13:52:00          已更新 
武汉  2009-9-21 13:53:00          31 

解决方案 »

  1.   

    sql我并不熟悉,不过这里有个比较笨的方法你这些数据库里存的时候应该有ID把首先aa = select max(id)然后update ...where id = aa 
      

  2.   

    --> Test Data: @t
    declare @t table ([CITY] varchar(4),[DATE] datetime,[C] varchar(20))
    insert into @t
    select '武汉','2009-9-21 13:45:00',32 union all
    select '长沙','2009-9-21 13:46:00',null union all
    select '长沙','2009-9-21 13:47:00',31 union all
    select '长沙','2009-9-21 13:48:00',null union all
    select '上海','2009-9-21 13:49:00',29 union all
    select '上海','2009-9-21 13:50:00',38 union all
    select '武汉','2009-9-21 13:51:00',31 union all
    select '上海','2009-9-21 13:52:00',null union all
    select '武汉','2009-9-21 13:53:00',31select * from @t
    --Code
    update a SET c='已更新' FROM @t a WHERE NOT EXISTS(SELECT 1 FROM @t WHERE city=a.CITY AND [date]>a.[date]) AND c IS NULLselect * from @t --Result
    /*
    CITY DATE                    C
    ---- ----------------------- --------------------
    武汉   2009-09-21 13:45:00.000 32
    长沙   2009-09-21 13:46:00.000 NULL
    长沙   2009-09-21 13:47:00.000 31
    长沙   2009-09-21 13:48:00.000 已更新
    上海   2009-09-21 13:49:00.000 29
    上海   2009-09-21 13:50:00.000 38
    武汉   2009-09-21 13:51:00.000 31
    上海   2009-09-21 13:52:00.000 已更新
    武汉   2009-09-21 13:53:00.000 31
    */
      

  3.   

    加个唯一标识列id,
    update tb set C = xxx
    where id in  
    (select top 1 id from tb 
    where C = null
    order by DATE )
      

  4.   

    假设表明为aaaupdate t1
    set t1.c='已更新' 
    from aaa t1
    where t1.c IS NULL and t1.DATE = (select max(DATE) from aaa where CITY=t1.CITY)
      

  5.   

    update a set c='xxxx' from 

    inner join 
    (select max(Id/*或者没有主键即用时间*/) as id,city from a group by city ) b
    on a.Id=b.Id
      

  6.   

    update tablename set C='aaaaaa' where DATE=(select MAX(DATE) from tablename where C is null) as AAAA
      

  7.   

    SQLSERVER:
    update a set a.C='已更新' from a inner join 
    (select max(DATE) as DATE1,CITY from a group by CITY ) b
    on a.CITY=b.CITY and a.DATE=b.DATE1MYSQL:update a inner join (select max(DATE) as DATE1,city from a group by CITY ) b on a.CITY=b.CITY and a.DATE=b.DATE1 set a.C='已更新'
      

  8.   

    SQLSERVER:
    update a set a.C='已更新' from a inner join 
    (select max(DATE) as DATE1,CITY from a group by CITY ) b
    on a.CITY=b.CITY and a.DATE=b.DATE1 where b.C IS NULL