利用SQL语句对不同数据库进行高效果分页
|
源代码网整理以下在程序的开发过程中,处理分页是大家接触比较频繁的事件,因为现在软件基本上都是与数据库进行挂钩的。但效率又是我们所追求的,如果是像原来那样把所有满足条件的记录全部都选择出来,再去进行分页处理,那么就会多多的浪费掉许多的系统处理时间。为了能够把效率提高,所以现在我们就只选择我们需要的数据,减少数据库的处理时间,以下就是常用SQL分页处理: 源代码网整理以下1、SQL Server、Access数据库 源代码网整理以下这都微软的数据库,都是一家人,基本的操作都是差不多,常采用如下分页语句: 源代码网整理以下PAGESIZE:每页显示的记录数 源代码网整理以下CURRENTPAGE:当前页号 源代码网整理以下数据表的名字是:components 源代码网整理以下索引主键字是:id 源代码网整理以下select top PAGESIZE * from components where id not in 源代码网整理以下(select top (PAGESIZE*(CURRENTPAGE-1)) 源代码网整理以下id from components order by id)order by id 源代码网整理以下如下列: 源代码网整理以下select top 10 * from components where id not in 源代码网整理以下(select top 10*10 id from components order by id) 源代码网整理以下order by id 源代码网整理以下从101条记录开始选择,只选择前面的10条记录 源代码网整理以下2、Oracle数据库 源代码网整理以下因为Oracle数据库没有Top关键字,所以这里就不能够像微软的数据据那样操作,这里有两种方法: 源代码网整理以下(1)、一种是利用相反的。 源代码网整理以下PAGESIZE:每页显示的记录数 源代码网整理以下CURRENTPAGE:当前页号 源代码网整理以下数据表的名字是:components 源代码网整理以下索引主键字是:id 源代码网整理以下select * from components where id not 源代码网整理以下in(select id from components where 源代码网整理以下rownum<=(PAGESIZE*(CURRENTPAGE-1))) 源代码网整理以下and rownum<=PAGESIZE order by id; 源代码网整理以下如下例: 源代码网整理以下select * from components where id not in 源代码网整理以下(select id from components where rownum<=100) 源代码网整理以下and rownum<=10 order by id; 源代码网整理以下从101到记录开始选择,选择前面10条。 源代码网整理以下 源代码网整理以下select * from components where rownum 源代码网整理以下<=(PAGESIZE*(CURRENTPAGE-1)) minus 源代码网整理以下select * from components where rownum 源代码网整理以下<=(PAGESIZE*(CURRENTPAGE-2)); 源代码网整理以下如例:select * from components where 源代码网整理以下rownum<=10 minus select * from components 源代码网整理以下where rownum<=5;. 源代码网整理以下(3)、一种是利用Oracle的rownum,这个是Oracle查询自动返回的序号,一般不显示,但是可以通过select rownum from [表名]看到,注意,它是从1到当前的记录总数。 源代码网整理以下select * from (select rownum tid,components. 源代码网整理以下* from components where rownum<=100) where tid<=10; 源代码网供稿. |
