首页 > 代码库 > 分页语句
分页语句
Mysql 分页方案:
select * from t order by id limit 10;
Oracle分页方案:
select * from (selcet rownum r,* from t order by where r<40 ) where r>30;
mysql:sql = "select * from articles limit " + (pageNo-1)*pageSize + "," + pageSize;
oracle: sql = "select * from " +
"(select rownum r,* from " +
"(select * from articles order by postime desc)" +
"where rownum<= " + pageNo*pageSize +") tmp " +
"where r>" + (pageNo-1)*pageSize;
注释: 第 7 行保证 rownum 的顺序是确定的, 因为 oracle 的索引会造成 rownum 返回不同的
值 简
洋提示: 没有 order by 时, rownum 按顺序输出, 一旦有了 order by, rownum 不按顺序输
出了, 这说明 rownum 是排序前的编号。 如果对 order by 从句中的字段建立了索引, 那么,
rownum 也是按顺序输出的, 因为这时候生成原始的查询结果集时会参照索引表的顺序来构
建。
sqlserver:sql = "select top 10 * from id not id(select top " + (pageNo-1)*pageSize + "id from
articles)"
DataSource ds = new InitialContext().lookup(jndiurl);
Connection cn = ds.getConnection();
//"select * from user where id=?" --->binary directive
PreparedStatement pstmt = cn.prepareSatement(sql);
ResultSet rs = pstmt.executeQuery()
while(rs.next())
{ o
ut.println(rs.getString(1));
}
分页语句