假设共50页,每页有8条数据,现在需要取第29页的数据:
需在程序中定义三个变量:pageSize、pageNumber和startNumber
pageSize:每页的数据量,此时应为8
pageNumber:当前页的页码,此时应为29
startNumber:当前页的起始数据号码,此时应为8*(29-1)+1=225
查询sql语句时,需要知道rownumber的起止值,此处定义为startNumber和endNumber
经计算可得:
startNumber=225,endNumber=225+8=232
==================================
在[b]DB2[/b]数据库中,分页sql的书写方式为:
select * from
(
select id, name, rownumber() over (order by id asc) as rowid
from db2.table_name
) temp
where temp.rowid >= startNumber and temp.rowid <= endNumber
======================================
在[b]Oracle[/b]数据库中,分页sql的书写方式为:
select * from
(
select temp.*, rownum rn
from (select * from table_name) temp
where rownum <= endNumber
)
where rn >= startNumber
==========================================
在[b]SQL Server[/b]数据库中,分页sql的书写方式为:
方式一:
select top pageSize * from table_name
where id not in
(
select top pageSize*(pageNumber-1) id
from table_name order by id asc
)
order by logid asc
方式二:
select top pageSize * from
(
select row_number() over (order by id asc) as rowid, * from table_name
) temp
where rowid >= startNumber and temp.rowid <= endNumber
需在程序中定义三个变量:pageSize、pageNumber和startNumber
pageSize:每页的数据量,此时应为8
pageNumber:当前页的页码,此时应为29
startNumber:当前页的起始数据号码,此时应为8*(29-1)+1=225
查询sql语句时,需要知道rownumber的起止值,此处定义为startNumber和endNumber
经计算可得:
startNumber=225,endNumber=225+8=232
==================================
在[b]DB2[/b]数据库中,分页sql的书写方式为:
select * from
(
select id, name, rownumber() over (order by id asc) as rowid
from db2.table_name
) temp
where temp.rowid >= startNumber and temp.rowid <= endNumber
======================================
在[b]Oracle[/b]数据库中,分页sql的书写方式为:
select * from
(
select temp.*, rownum rn
from (select * from table_name) temp
where rownum <= endNumber
)
where rn >= startNumber
==========================================
在[b]SQL Server[/b]数据库中,分页sql的书写方式为:
方式一:
select top pageSize * from table_name
where id not in
(
select top pageSize*(pageNumber-1) id
from table_name order by id asc
)
order by logid asc
方式二:
select top pageSize * from
(
select row_number() over (order by id asc) as rowid, * from table_name
) temp
where rowid >= startNumber and temp.rowid <= endNumber