小技巧收集(7)-SQL分页算法

1 使用Row_Number

with  t_pager  as  ( select  myIndex  =  ROW_NUMBER()  OVER  ( ORDER   BY  TableColumns), *   from  class  where )
select   *   from  t_pager  where  myIndex  between  StartIndex  and  EndIndex
 
 
2 使用表变量
declare   @tt   table (id  int   identity ( 1 , 1 ),nid  int )
insert   into   @tt (nid) 
select   top  EndIndex class_id  from  class

select  a. *   from  class a, @tt  b  where  a.class_id = b.nid
and  b.id  between  StartIndex  and  EndIndex  order   by  b.id
 

 3 TopInTop

select   top  PageNums  *   from  class 
    
where  Table_id  in  (
        
select   top  EndIndex Table_id  from  class
        
order   by  Table_id  desc
    ) 
    
order   by  Table_id

你可能感兴趣的:(sql)