Returning TOP N Records

本文介绍了不同数据库平台中限制SQL查询返回记录数的方法,如Microsoft SQL Server使用TOP子句,PostgreSQL和MySQL使用LIMIT,Oracle使用ROWNUM等。还讨论了保持代码数据库独立性的方法及其可能的性能影响。

Returning only the first N records in a SQL query differs quite a bit between database platforms. Here's some samples:

Microsoft SQL Server

SELECT TOP 10 column FROM table 

PostgreSQL and MySQL

SELECT column FROM table LIMIT 10 

Oracle

SELECT column FROM table WHERE ROWNUM <= 10 

Sybase

SET rowcount 10 SELECT column FROM table 

Firebird

SELECT FIRST 10 column  FROM table 

Due to these differences if you want to keep your code database independent you should use the maxrows attribute in the cfquery tag in ColdFusion. The tradeoffs to database independance is performance, I would expect maxrows to be slower than specifying the rows in the SQL.

<cfquery datasource="#ds#" maxrows="10"> SELECT column FROM table </cfquery> 

PostgreSQL and MySQL have a cool feature that will let you return an arbitrary range of rows (eg return rows 10-20). This is very handy for displaying pages of records:

SELECT column FROM table LIMIT 10 OFFSET 20 

The above query will return rows 20-30

转载于:https://www.cnblogs.com/binlyzhuo/archive/2010/06/25/1765064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值