mssql 实现mysql limit sql语句
mssql 实现mysql教程 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
补充:数据库,Mssql