Home > Database > Mysql Tutorial > mssql 实现mysql limit sql语句

mssql 实现mysql limit sql语句

WBOY
Release: 2016-06-07 17:48:06
Original
1584 people have browsed it

mssql 实现 limit sql语句
在mysql中,可以用 limit 来查询第 m 列到第 n 列的记录,例如:

select * from tablename limit m, n

但是,在sql server中,不支持 limit 语句。怎么办呢?

解决方案:

虽然sql server不支持 limit ,但是它支持 top。

我们以sql server 2005为例,就以它自带的示范 adventureworks 作为测试数据:

select addressid, city, stateprovinceid, postalcode
from person.[address]
where stateprovinceid = 58
order by addressid

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

select top 6 addressid, city, stateprovinceid, postalcode
from person.[address]
where stateprovinceid = 58
order by addressid

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

select top 3 addressid, city, stateprovinceid, postalcode
from person.[address]
where stateprovinceid = 58
and addressid not in (
  select top 6 addressid from person.[address] where stateprovinceid = 58 order by addressid
)
order by addressid

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