建站常识

sql利用ID大于多少和SELECT TOP分页

发布时间 | 2017/8/13  点击 | 

sql分页:利用ID大于多少和SELECT TOP分页
 程序代码部分:

SELECT TOP 页大小 *  

FROM TestTable  

WHERE (ID > (SELECT MAX(id) FROM (SELECT TOP 页大小*页数 id FROM 表 ORDER BY id) AS T))  

ORDER BY ID

ASP超高效分页算法
<%
'每页的记录数
dim pagesize
pagesize= "30" 
'读出总记录数,总页数
Dim TotalRecords,TotalPages
SQLstr="Select count(id) As RecordSum From table1"
Set Rs=conn.Execute(SQLstr,0,1) 
TotalRecords=Rs("RecordSum") 
if Int(TotalRecords/pagesize)=TotalRecords/pagesize then
TotalPages=TotalRecords/pagesize
else
TotalPages=Int(TotalRecords/pagesize)+1
end if
Rs.Close 
Set Rs=Nothing 
'当前页码
dim page
page=Request("page")
if isnumeric(page)=false then
response.write "<SCRIPT language=JavaScript>alert('参数错误!');"
response.write "window.close();</SCRIPT>"
response.end
end if
If page="" or page<1 Then page=1
If page-TotalPages>0 Then page=TotalPages
page=int(page)

if page=1 then
sql="select top "&pagesize&" id,title,time from table1 order by time desc"
else
sql="select top "&pagesize&" id,title,time from table1 where time<(SELECT Min(time) FROM (SELECT TOP "&pagesize*(page-1)&" time FROM table1 ORDER BY time desc) AS T) order by time desc"
end if
Set rs = Server.CreateObject ("ADODB.Recordset")
rs.Open sql,conn,1,1
Do While Not rs.Eof
response.write "每条记录信息:"&rs("id")&"<br>"
rs.movenext
loop
rs.close
set rs=nothing
''翻页代码省略……
%> 

相关信息