时间:2021-07-01 10:21:17 帮助过:46人阅读
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