sql 倒序取第几行到第几行 对正序的数据倒序排列

select   b.*   from ( select   top   10   a.*   from   ( select   top   20   *   from   HaoMa   order   by   hm_id   asc )   a   order   by   a.hm_id   desc ) b order   by   b.hm_id  desc 


是不是看着眼晕,那这样写

select   b.*   from 
( 
select   top   5   a.*   from   
( 
select   top   10   *   from   customers   order   by   CustomerID   asc 
)   a   order   by   a.CustomerID   desc 
) b
order   by   b.CustomerID    


这就明白了吧,详细的介绍如下:  fromhttp://www.cnblogs.com/gossip/archive/2011/04/16/2018099.html

SQL SERVER取第几行到第几行的方法(包括2000和2005+)

 

--SQL SERVER 2000 取第几行到第几行的方法

--(第一种方法效率最差

select   top   5   *   from   customers   where   

CustomerID   not   in   (select   top   5   CustomerID   from   customers) 

 

 

--(第二种方法)  效率最好, 这里先执行的是Order by 然后才执行Top

select   b.*   from 

( 

select   top   5   a.*   from   

( 

select   top   10   *   from   customers   order   by   CustomerID   asc 

)   a   order   by   a.CustomerID   desc 

) b

order   by   b.CustomerID    

 

 

--(第三种方法利用临时表和identity(int,1,1

DROP TABLE #temp

select  identity(int,1,1) as rowID,*   into   #temp   from   customers 

SELECT * FROM #temp WHERE rowID>AND rowID<=10

--SQL SERVER 2000 取第几行到第几行的方法

 

 

--SQL SERVER 2005+ 取第几行到第几行的方法

SELECT b.* FROM 

(

SELECT ROW_NUMBER() OVER(ORDER BY c.customerid) rowIndex,* FROM Customers c

) b

WHERE b.rowIndex>AND b.rowindex<=10

--SQL SERVER 2005+ 取第几行到第几行的方法

 

 

 

 

 

 

 

 

----------------------------------------------------------

 对正序的数据倒序排列:

select * from( select top 20 * from HaoMa order by hm_id )a  order by hm_id desc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值