SQL更新最新一条数据怎么写?
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:

更新最新一条数据的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

已经有11 个评论
  1. wuwei04 说:

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

  2. 萌芽邪恶 说:

    用SQL Server的isnull函数

  3. look 说:

    SQL code
    –> 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′,31

    select * 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 NULL

    select * 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
    */

  4. nashliu 说:

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

  5. nashliu 说:

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

  6. 徐咏波 说:

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

  7. forestyaser 说:

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

  8. mikeyao210 说:

    楼上的绝对可以

  9. wtaxum 说:

    update tablename set C=’aaaaaa’ where DATE=(select MAX(DATE) from tablename where C is null) as AAAA这个测试行

  10. nashliu 说:

    SQLSERVER:update a set a.C=’已更新’ from a inner join  (select max(DATE) as DATE1,CITY from a group by CITY ) bon 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=’已更新’

  11. xjhuc05 说:

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

我要评论

评论功能因故关闭!

请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)


返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 友情链接 | 版权声明