db2分页技术优化问题:

在SQL语句后面加入了 " order by aa asc fetch first 20 rows only " 取0-20的数据 当取第一页的时候会显示20条数据,但是当点击下一页的时候(20-40)数据就为空了,其实是有数据的。可能跟 fetch first 20 有关,但是必须得加上这句话,请问各位大侠有没有什么解决办法啊。

fetch first 20  rows only 是提取前20条数据的不能作为分页来使用。

分页有三种:

1、 如果一定要使用fetch first  20  rows only  的方式,可以使用WITH AS 方式实现:

-------------------------------------------------

WITH TMP AS(

select * from (

select *,rownumber() over(order by id asc ) as row_id from table_name

)  where  row_id > (pagenumber-1)*pagesize

)

SELECT TMP.* FROM TMP WHERE   fetch first  pagesize  rows only

-------------------------------------------------

2、比较常见的SQL分页,可以使用沈阳java网友的写法:

-----------------------------------------------------

select * from (

select *,rownumber() over(order by id asc ) as rowid from table_name

)as a

where a.rowid >= (pagenumber-1)*pagesize+1  AND a.rowid <= pagenumber*pagesize

-------------------------------------------------------

3、使用PageHelper分页插件,也可以实现自动分页:

使用方法

--------------------------------------------------------------

a.引配置

<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">

</plugins>

-------------------------------------------------------------

b.使用:

LogInfoQuery example = new LogInfoQuery();

example.setOrderByClause(" id desc ");

Criteria criteria = example.createCriteria();

if(null != logInfo.getStartTimeQuery() && null != logInfo.getEndTimeQuery()) {

criteria.andOpTimeBetween(logInfo.getStartTimeQuery(), logInfo.getEndTimeQuery());

}

PageHelper.startPage(page.getPageNo(), page.getPageSize());

List<LogInfo> result = logInfoMapper.selectByExample(example);

PageInfo<LogInfo> pageInfo = new PageInfo<>(result);

return pageInfo;

-------------------------------------------

使用示例结束。

如果不懂可以追问。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-13
select * from (select 字段1,字段2,字段3,字段4,字段5,rownumber() over(order by 排序字段 asc ) as rowid from 表名 )as a where a.rowid >= startPage AND a.rowid <endPage本回答被提问者采纳
第2个回答  2011-04-06
和 fetch first 20 rows only 没有关系