5-在ORACLE中实现SELECT TOP N

前端之家收集整理的这篇文章主要介绍了5-在ORACLE中实现SELECT TOP N前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于ORACLE不支持SELECT TOP语句,所以在ORACLE中经常是用ORDER BY跟ROWNUM的组合来实现SELECT TOP N的查询

简单地说,实现方法如下所示:
SELECT 列名1...列名n FROM
(SELECT 列名1...列名n FROM 表名 ORDER BY 列名1...列名n)
WHERE ROWNUM <= N(抽出记录数)
ORDER BY ROWNUM ASC
下面举个例子简单说明一下。
顾客表customer(id,name)有如下数据:
ID NAME
01 first
02 Second
03 third
04 forth
05 fifth
06 sixth
07 seventh
08 eighth
09 ninth
10 tenth
11 last
则按NAME的字母顺抽出前三个顾客的sql语句如下所示:
SELECT * FROM
(SELECT * FROM CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= 3
输出结果为:
01 first 原文链接:https://www.f2er.com/oracle/210906.html

猜你在找的Oracle相关文章