手机
当前位置:查字典教程网 >编程开发 >mysql数据库 >如何在SQL Server中实现 Limit m,n 的功能
如何在SQL Server中实现 Limit m,n 的功能
摘要:在MySQL中,可以用Limit来查询第m列到第n列的记录,例如:复制代码代码如下:select*fromtablenamelimitm,n...

在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

)

【如何在SQL Server中实现 Limit m,n 的功能】相关文章:

MySQL 实用命令

一次性备份SQL Server中所有的数据

保护SQL Server 2000安全性的十个步骤

如何处理MySQL密码有效性验证失败

Lost connection to MySQL server during query的解决

从MySQL得到最大的优化性能

如何确定SQL Server栏中的最大值

如何进行SQL Server批理修改权限

如何在MySQL中复位根用户密码

oracle中的connect by 在sql server中实现

精品推荐
分类导航