查询数据库中满足条件的特定行数据

查询数据库中满足条件的特定行数据,在这里主要给出三条查询语句,其中第三条主要是针对SQL Server2005数据库的,因为其中的Row_Number()函数只有在SQL Server2005中才支持。
例子:
我数据库中有一个table表,表中一共有50条数据,我现在要查询第21到30条数据,我可以对这50条数据分成5页,每页10条数据。
一、select top 页大小 * from table1 where (id not in (select top (页大小-1)*每页数 id from 表 order by id))order by id
例子:select top 10 * from table where (id not in (select top 20 id from table order by id))order by id

二、select top 页大小 * from table1 where id>(select max (id) from (select top ((页码-1)*页大小) id from table1 order by id) as t) order by id
例子:select top 10 * from table where id>(select max (id) from (select top 20 id from table order by id) as t) order by id

总结:二比一好,not in费时

三、select * from(select ROW_NUMBER() over(order by id) -1 as rownum,table *  from
依据什么排序 默认行号为-1+1=0 table) as d where rownum between 0 and 10 起始行 显示多少行

例子:select * from(select ROW_NUMBER() over(order by ID desc) as rownum,table *  from table) as d where rownum between 21 and 30

posted on 2007-06-20 16:24  Edwin dong  阅读(1016)  评论(0编辑  收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>