查询数据库中满足条件的特定行数据

本文提供了三种SQL分页查询的方法,适用于不同的数据库环境,特别是针对SQL Server 2005的查询技巧,利用ROW_NUMBER()函数实现高效的数据分页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查询数据库中满足条件的特定行数据,在这里主要给出三条查询语句,其中第三条主要是针对SQL Server2005数据库的,因为其中的Row_Number()函数只有在SQL Server2005中才支持。
例子:
我数据库中有一个table表,表中一共有50条数据,我现在要查询第21到30条数据,我可以对这50条数据分成5页,每页10条数据。
一、select top 页大小 * from table1 where (id not in (select top (页大小-1)*每页数 id from 表 order by id))order by id
例子:select top 10 * from table where (id not in (select top 20 id from table order by id))order by id

二、select top 页大小 * from table1 where id>(select max (id) from (select top ((页码-1)*页大小) id from table1 order by id) as t) order by id
例子:select top 10 * from table where id>(select max (id) from (select top 20 id from table order by id) as t) order by id

总结:二比一好,not in费时

三、select * from(select ROW_NUMBER() over(order by id) -1 as rownum,table *  from
依据什么排序 默认行号为-1+1=0 table) as d where rownum between 0 and 10 起始行 显示多少行

例子:select * from(select ROW_NUMBER() over(order by ID desc) as rownum,table *  from table) as d where rownum between 21 and 30

转载于:https://www.cnblogs.com/dongyongjing/archive/2007/06/20/790627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值