萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> sqlserver的3種分頁方式

sqlserver的3種分頁方式

   sqlserver的3種分頁方式,如下:

  01 ---sqlServer 2005 分頁語句集合

  02 ----缺點:

  03 -- top:必須用戶編寫復雜sql,不支持復合主鍵

  04 -- max:必須用戶編寫復雜sql,不支持非唯一列排序

  05 --row:不支持sqlserver2000

  06 --------------------------------

  07 select TOP @pagesize id,email,qq,wechart,phone,phone1 FROM contact

  08 where id not in

  09 (SELECT TOP (@pagesize*(currentpage-1)) id from contact ORDER BY id ASC )

  10 ORDER BY id ASC

  11

  12

  13 -- 效率最低下的分頁語句(只需要知道頁數和每頁的顯示數目即可)

  14 select * from AISINO_BD_TelephoneRecord order by ID asc;

  15 select top 5 ID ,companyID,projectID from dbo.AISINO_BD_TelephoneRecord

  16 where ID not in(

  17 select top (5*(2-1)) ID from AISINO_BD_TelephoneRecord order by ID asc

  18 ) order by ID asc

  19

  20

  21 ----------------------

  第二種方法,只需要知道頁數和每頁的顯示數目即可-------------------------------

  22

  23 select top 5 * from AISINO_BD_TelephoneRecord

  24 where ID>(

  25 select max(ID)

  26 from (select top (5*(2-1)) ID from AISINO_BD_TelephoneRecord order by ID asc)tt

  27 )

  28 order by ID asc

  29

  30

  31 ------------------

  -第三種方法---只需要知道頁數和每頁的顯示數目即可----------------------------------

  32

  33

  34 select *

  35 from (select top (5*(1-1)+5) row_number()over(order by Id asc)__rn__, * from AISINO_BD_TelephoneRecord)t

  36 where __rn__>5*(1-1)

  在oracle 中的分頁:

  oracle 自帶了rownum ,直接使用rownum 進行分頁:

  1

  select *

  2

  from (select a.*, rownum nm

  3

  from (select * from aos_rms_user) a

  4

  where rownum <= 5*(1-1)+5)

  5

  where nm >= 5*(1-1)

copyright © 萬盛學電腦網 all rights reserved