n (n-1)*10+1 n*10
select * from student where sno>=(n-1)*10+1 and sno<=n*10; 写法前提:必须id连续,否则无法满足每页显示10个数据
select rownum,t* from student t where rownum>=(n-1)*10+1 and rownum<=n*10 order by t.sno
--1如果根据sno排序则rownum会混乱(解决方案:分开使用->先只排序,再只查询rownum)2rownum不能查询>数据
//ORACLE/SqlServer都是从1开始计数:
oracle的分页查询语句
select * from
(
select rownum r,t.* from
(select s.* from student s order by sno asc) t
where rownum<=n*10;
)
where r>=(n-1)*10+1;
优化:
select * from
(
select rownum r,t.* from
(select s.* from student s order by sno asc) t
where rownum<=页数*页面大小
)
where r>=(页数-1)*页面大小+1;
SQLServer分页:3种分页sql
row_number() over(字段);
sqlserver2003:top --此种分页Sql存在弊端(如果id值不连续,则不能保证每页数据量相等)
select top 页面大小 * from student where id not
(select top (页数-1)*页面大小 id from student order by sno asc)
sqlserver2005之后支持:
select * from
(
select row_number() over(sno order by sno asc) as r,* from student
where r<=n*10
)where r>=(n-1)*10+1;
SQLServer此种分页sql与Oracle分页sql的区别;
1.rownum,row_number()
2.oracle需要排序(为了 排序,单独写了一个子查询),但是在sqlserver中可以省略该排序的子查询 因为SQL server中可以通过over直接排序