有一张表Id字段,主键自增长,我想按他的顺序先倒序desc,然后再用between and条件,得到相应的数据,如何写呢?先谢了
用top和行号两种方式,where id 分页不可取,不一定连续的
SELECT * FROM
(SELECT * ,ROW_NUMBER() OVER(ORDER BY ClassId) AS WOR
FROM ClassTable ) D
WHERE WOR BETWEEN 1 AND 5
select * from (select id,name from test t order by id) where id=50978
先order再where 没有实际意义,SQL实际查询会先where再排序,如果在SQL server 2008中,可以这样
SELECT * FROM
(SELECT TOP (100) PERCENT * FROM Trade ORDER BY T_ID DESC) AS t
WHERE (T_ID BETWEEN 6810 AND 7000)
更详细的用法,请参考
http://www.cnblogs.com/downmoon/archive/2010/10/12/1849248.html