日期:2014-05-17  浏览次数:20527 次

问一个分页倒序SQL语句的问题
我写了一个这样的,每页显示两条,现在显示第二页的,我这样看上去结果是对的,我想问如果要倒序的话应该怎么写,order by newsid desc不知道放哪里


select top 4 * from (select top 100 percent * from news) a
except
select top 2 * from (select top 100 percent * from news) a 

求教,顺便问一下别的方法分页语句或者存储过程分页,感觉我这样效率比较慢

------解决方案--------------------
SQL code

select * from (
    select row_number() over(order by getdate()) rn,* from news) t
    where rn between 3 and 4

------解决方案--------------------
分库的话,这样
select * from [数据库名]..[表名]

SQL code

--getdate()换为你的排序列,不知你有哪些列,所以我没换
select * from (
    select row_number() over(order by getdate()) rn,* from [数据库名]..news) t
    where rn between 3 and 4

------解决方案--------------------
个人建议 分页还是用临时表吧!你top的话 数据太多了会有点小问题

with tableName (column1,column2,.....)
as
(select Row_Nmber() over(order by aa desc) c1,c2,..... +)
select * from TableName + where ....
------解决方案--------------------
order by newsid desc 放到下面例子的位置,看看
SQL code

select * from (
select top 4 * from (select top 100 percent * from news) a order by newsid desc)b
except
select * from (select top 2 * from (select top 100 percent * from news) a order by newsid desc)c

------解决方案--------------------
SELECT 。。。 FROM ( SELECT ROW_NUMBER() OVER(ORDER BY newsid desc) AS RowNumber, 。。。。FROM news