当前位置:Gxlcms > mysql > mssql实现mysqllimitsql语句

mssql实现mysqllimitsql语句

时间:2021-07-01 10:21:17 帮助过:46人阅读

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

人气教程排行