sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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>
posted on   sunny123456  阅读(1867)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示