如何用sql语句 实现分页查询?

我制作了一个数据库其中一张表
create table news
(
news_id int primary key identity(1,1),
news_title varchar(50) not null,
news_author varchar(20) ,
news_summary varchar(50),
news_content text not null,
news_pic varchar(50)
)
然后插入数据 20条
我想要制作一个分页,每页显示5条记录。
说的跟清晰一点就是 我想要创建查询,第一次查询从0-5条,第二次查询从5-10条,第三次查询出 10-15条
在清楚一点就是如果用mysql 写就是 select * from news_type limit m,size; m为从m+1条开始查询,sizs为查询的条数。这句话用sqlserver 要如何实现?
我自己解决了,摸索了2个多小时终于搞定了
select top 10 * from 表名--查询显示0-10条记录(10条)
select top 10 *from 表名 where 主键 not in(select top 10 表名 from 主键);--查询显示11-20条记录(10条)
select top 10* from表名 where 主键not in (select top 20 表名 from 主键);--查询显示21-30条记录(10条)
select top 10* from表名 where 主键not in (select top 30表名 from 主键);--查询显示31-40条记录(10条)

慕码人2483693
浏览 876回答 3
3回答

呼唤远方

方法1:适用于 SQL Server 2000/2005SELECT TOP 页大小 *FROM table1WHERE id NOT IN(SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id)ORDER BY id方法2:适用于 SQL Server 2000/2005SELECT TOP 页大小 *FROM table1WHERE id >(SELECT ISNULL(MAX(id),0)FROM(SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id) A)ORDER BY id方法3:适用于 SQL Server 2005SELECT TOP 页大小 *FROM(SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1) AWHERE RowNumber > 页大小*(页数-1)

慕容708150

适用于 SQL Server 2000/2005SELECT TOP 页大小 *FROM table1WHERE id NOT INSELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id

料青山看我应如是

oracle :select * from (select *,rownum from table order by culm1) A where A.rownum>10 and A.rownum<20sql server :select top 10 * from (select top 20 * from table1 order by culm1) order by culm1 desc
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server