Home > Database > Mysql Tutorial > 如何在SQL Server中实现 Limit m,n 的功能_MySQL

如何在SQL Server中实现 Limit m,n 的功能_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:24:40
Original
883 people have browsed it

bitsCN.com 在MySQL中,可以用 Limit 来查询第 m 列到第 n 列的记录,例如:

select * from tablename limit m, n

但是,在SQL Server中,不支持 Limit 语句。怎么办呢?
解决方案:
虽然SQL Server不支持 Limit ,但是它支持 TOP。
我们以SQL Server 2005为例,就以它自带的示范数据库 AdventureWorks 作为测试数据:

select id from tablename

如果要查询上述结果中前6条记录,则相应的SQL语句是:

select top 6 id from tablename

如果要查询上述结果中第 7 条到第 9 条记录,则相应的SQL语句是:

select top 3 id from tablename
where id not in (
  select top 6 id from tablename
)


select top (n-m+1) id from tablename
where id not in (
  select top m-1 id from tablename
)


select top @pageSize id from tablename
where id not in (
  select top @offset id from tablename
)

bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template