table1表里有60条记录
1
2
4
7
.
.
.
80  id从小到大 但不是连续的我要检索其中的10条 条件是 (大于等于20  并且按 降序排列 id是最接近20的那些)
就是这些的记录 这样排
40
38
37
33
32
29
27
25
24
20我写了这样的 select top 10 * from table1 where id>=20 order by id desc
可是这样检索出来的记录是从最大的开始的 不是最接近20的那10条
如果有 select bottom 10 * from table1 where id>=20  order by id desc就可以了 但没这样的sql语句。
谁能告诉我怎么写么?我在分页的时候用到的

解决方案 »

  1.   

    select * from table1 where id in
       (Select top 10 id from Table1 where id >= 20 order by id)
    order by id desc
      

  2.   

    select top 10 * from table1 where id>=20 order by id asc注意是asc
      

  3.   

    和恐龙差不多,SELECT *
    FROM (SELECT TOP (10) * FROM Table1 WHERE (ID >= 20) ORDER BY ID) AS x
    ORDER BY ID DESC
      

  4.   

    select top 10 * from table1 where id>=20 order by id asc
    是升序排列了  我要的是降序列最后一只恐龙 和马客的 可以哦^-^我只不过想自定义分页 有个 首页 上一页 下一页 尾页 跳转到 页
    我现在用DataReader 用定制的sql语句 循环出想要哪页
    很烦琐谁有好的自定义分页的代码么
      

  5.   

    我现在在开发自己的Grid控件,也涉及到分页,没有具体思路,模糊印象是在Datatable中分...