1.有数据表A,有一个字段LASTUPDATETIME,是最后更新的时间,如果要查最新更新过的记录,如何写SQL语句.
2.要查数据表中第30到40条记录,有字段ID,但是ID并不连续!

解决方案 »

  1.   

    1.
    select * from tb order by lastupdatetime desc2.
    select top 10 * from tb where id not in(select top 30 id from tb)
      

  2.   

    select max(lastupdatetime) from tb
    select * from (select *,row_number() over(order by id)as px from tb)t where px between 30 and 40
      

  3.   

    1.有数据表A,有一个字段LASTUPDATETIME,是最后更新的时间,如果要查最新更新过的记录,如何写SQL语句. 
    select * from t  where LASTUPDATETIME =(select max(LASTUPDATETIME) from t)
    --or
    select * from t  a where not exists(select 1 from t where id=a.id and LASTUPDATETIME<a.LASTUPDATETIME)
    --2.要查数据表中第30到40条记录,有字段ID,但是ID并不连续!select top 10 * from (select top 40 * from t oder by 关键字 desc ) oder by 关键字 
      

  4.   

    1.有数据表A,有一个字段LASTUPDATETIME,是最后更新的时间,如果要查最新更新过的记录,如何写SQL语句.
    select *
    from [数据表A]
    where LASTUPDATETIME = (select max(LASTUPDATETIME) from [数据表A])2.要查数据表中第30到40条记录,有字段ID,但是ID并不连续!
    select * from tb a
    where exists (select 1 from (
        select top 10 id from (select top 40 id from tb order by id desc) as c
      ) as b where b.id=a.id)
      

  5.   

    --2.要查数据表中第30到40条记录,有字段ID,但是ID并不连续!
    select top 10 *
    from 
        (
         select * from (select top 40 * from t)a 
        )a
    order by id desc
      

  6.   

    1.有数据表A,有一个字段LASTUPDATETIME,是最后更新的时间,如果要查最新更新过的记录,如何写SQL语句. select t.* from tb t where LASTUPDATETIME = (select max(LASTUPDATETIME) from tb)
    2.要查数据表中第30到40条记录,有字段ID,但是ID并不连续!取n到m条记录的语句1.
    select top m * from tablename where id not in (select top n id from tablename)2.
    select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
    set rowcount n
    select * from 表变量 order by columnname desc3.
    select top n * from 
    (select top m * from tablename order by columnname) a
    order by columnname desc
    4.如果tablename里没有其他identity列,那么:
    select identity(int) id0,* into #temp from tablename取n到m条的语句为:
    select * from #temp where id0 >=n and id0 <= m如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
    exec sp_dboption 你的DB名字,'select into/bulkcopy',true
    5.如果表里有identity属性,那么简单:
    select * from tablename where identitycol between n and m