SQL Server中的分页查询 https://blog.csdn.net/tswc_byy/article/details/82053091
零、码仙励志
比我差的人还没放弃,比我好的人仍在努力,我就更没资格说我无能为力
一、建库和建表
- create database scort
- use scort
- create table emp
- (
- empno int primary key,
- ename nvarchar( 10),
- sal int,
- deptno int
- )
- insert into emp values ( 7369, ‘smith‘, 800, 20);
- insert into emp values ( 7499, ‘allen‘, 1600, 10);
- insert into emp values ( 7521, ‘ward‘, 1250, 30);
- insert into emp values ( 7566, ‘jones‘, 2975, 30);
- insert into emp values ( 7654, ‘martin‘, 1250, 10);
- insert into emp values ( 7698, ‘blake‘, 2850, 30);
- insert into emp values ( 7782, ‘clark‘, 2450, 20);
- insert into emp values ( 7788, ‘scott‘, 3000, 20);
- insert into emp values ( 7839, ‘king‘, 5000, 10);
- insert into emp values ( 7844, ‘turn‘, 1500, 10);
- insert into emp values ( 7876, ‘adams‘, 1100, 30);
二、分页查询的用法
1.公式:
- 假设每页显示n条记录,当前显示的是第m页,表名是A,主键是A_id
- select top n *
- from A
- where A_id not in ( select top (m -1)*n A_id from A)
2.实例演示:
- sal从高到低排序,输出1-3条记录的信息
- select top 3 * from emp order by sal desc
- sal从高到低排序,输出4-6条记录的信息
- select top 3 *
- from emp
- where sal not in ( select top 3 sal from emp order by sal desc)
- order by sal desc
- sal从高到低排序,输出7-9 条记录的信息
- select top 3 *
- from emp
- where sal not in ( select top 6 sal from emp order by sal desc)
- order by sal desc
- sal从高到低排序,输出10-12条记录的信息
- (因为没有第12条记录,所以只显示了2条)
- select top 3 *
- from emp
- where sal not in ( select top 9 sal from emp order by sal desc)
- order by sal desc
本篇博客来自于郝斌老师视频教程的总结以及笔记的整理,仅供学习交流,切勿用于商业用途,如有侵权,请联系博主删除,博主QQ:194760901
</div>
评论(
0)