sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

SQL Server中的分页查询 https://blog.csdn.net/tswc_byy/article/details/82053091

零、码仙励志

比我差的人还没放弃,比我好的人仍在努力,我就更没资格说我无能为力

一、建库和建表

  1. create database scort
  2. use scort
  3. create table emp
  4. (
  5. empno int primary key,
  6. ename nvarchar(10),
  7. sal int,
  8. deptno int
  9. )
  10. insert into emp values (7369,'smith',800,20);
  11. insert into emp values (7499,'allen',1600,10);
  12. insert into emp values (7521,'ward',1250,30);
  13. insert into emp values (7566,'jones',2975,30);
  14. insert into emp values (7654,'martin',1250,10);
  15. insert into emp values (7698,'blake',2850,30);
  16. insert into emp values (7782,'clark',2450,20);
  17. insert into emp values (7788,'scott',3000,20);
  18. insert into emp values (7839,'king',5000,10);
  19. insert into emp values (7844,'turn',1500,10);
  20. insert into emp values (7876,'adams',1100,30);

二、分页查询的用法

1.公式:

  1. 假设每页显示n条记录,当前显示的是第m页,表名是A,主键是A_id
  2. select top n *
  3. from A
  4. where A_id not in (select top (m-1)*n A_id from A)

2.实例演示:

  1. sal从高到低排序,输出1-3条记录的信息
  2. select top 3 * from emp order by sal desc

  1. sal从高到低排序,输出4-6条记录的信息
  2. select top 3 *
  3. from emp
  4. where sal not in (select top 3 sal from emp order by sal desc)
  5. order by sal desc

  1. sal从高到低排序,输出7-9 条记录的信息
  2. select top 3 *
  3. from emp
  4. where sal not in (select top 6 sal from emp order by sal desc)
  5. order by sal desc

  1. sal从高到低排序,输出10-12条记录的信息
  2. (因为没有第12条记录,所以只显示了2条)
  3. select top 3 *
  4. from emp
  5. where sal not in (select top 9 sal from emp order by sal desc)
  6. order by sal desc

本篇博客来自于郝斌老师视频教程的总结以及笔记的整理,仅供学习交流,切勿用于商业用途,如有侵权,请联系博主删除,博主QQ:194760901 

                </div>
posted on 2020-01-19 20:42  sunny123456  阅读(1842)  评论(0编辑  收藏  举报