比如在数据库中有 id  name
1   ds
2   fds
3   ...
4   ...
5   ...
6   ...
8   ...
9   ...如果想获取id中编号最大的前5项,就是获取id为9 8 6 5 4 这些,应该如何写这个SQL语句呢?

解决方案 »

  1.   

    select top 5 id from 表 order by id desc
      

  2.   

    SELECT TOP 5 * FROM table ORDER BY ID DESC
      

  3.   

    declare @T table(id int,name varchar(10))
    insert into @t select 1   ,'ds'
    union all select 2   ,'fds'
    union all select 3   ,'...'
    union all select 4   ,'...'
    union all select 5   ,'...'
    union all select 6   ,'...'
    union all select 8   ,'...'
    union all select 9   ,'...'select top 5 * from @t order by id desc