[英]Get range of records from SQL query Oracle
I want to use this SQL query to get only the records between 8 and 10:我想使用这个 SQL 查询只获取 8 到 10 之间的记录:
select *
from(
SELECT a.*,rownum rn
FROM ACTIVESESSIONSLOG a
ORDER BY USERID ASC)
WHERE rn >= 8 and rn <= 10
When I implement this SQL query into pagination I get every time 1 row on the second page no matter how many rows I have configured to be displayed into the pages.当我将此 SQL 查询实施到分页中时,无论我配置了多少行显示到页面中,我每次都会在第二页上获得 1 行。 Is this SQL query valid?这个 SQL 查询有效吗?
This is the table structure:这是表结构:
-- TABLE ACTIVESESSIONSLOG
CREATE TABLE ACTIVESESSIONSLOG(
ASESSIONID VARCHAR2(30 ) NOT NULL,
USERID VARCHAR2(30 ),
ACTIVITYSTART TIMESTAMP(6),
ACTIVITYEND TIMESTAMP(6),
ACTIVITY CLOB
)
/
Best wishes最良好的祝愿
rownum
is applied before the ORDER BY
so your query is almost certainly not doing what you expect. rownum
在ORDER BY
之前应用,因此您的查询几乎可以肯定没有按照您的预期进行。 Your query is essentially asking for an arbitrary 3 rows and the ORDER BY
isn't doing anything useful.您的查询本质上是要求任意 3 行,而ORDER BY
没有做任何有用的事情。
You could use the analytic function row_number
instead, ie您可以改用解析 function row_number
,即
SELECT *
FROM (SELECT a.*,
row_number() over (order by userid asc) rn
FROM activeSessionsLog a)
WHERE rn BETWEEN 8 AND 10
which will page through the results这将翻阅结果
SQL> ed
Wrote file afiedt.buf
1 select empno, ename, job
2 from (select e.*,
3 row_number() over (order by empno) rn
4 from emp e)
5* where rn between 1 and 3
SQL> /
EMPNO ENAME JOB
---------- ---------- ---------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
SQL> ed
Wrote file afiedt.buf
1 select empno, ename, job
2 from (select e.*,
3 row_number() over (order by empno) rn
4 from emp e)
5* where rn between 4 and 8
SQL> /
EMPNO ENAME JOB
---------- ---------- ---------
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
SQL> ed
Wrote file afiedt.buf
1 select empno, ename, job
2 from (select e.*,
3 row_number() over (order by empno) rn
4 from emp e)
5* where rn between 9 and 11
SQL> /
EMPNO ENAME JOB
---------- ---------- ---------
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK
It may be more efficient, however, to do something like this where Oracle can use the inner rownum <= 10
predicate to know that it can stop sorting the data once it has identified the first 10 rows.然而,执行这样的操作可能更有效,其中 Oracle 可以使用内部rownum <= 10
谓词来知道它可以在识别前 10 行后停止对数据进行排序。
SELECT c.*
FROM (SELECT b.*, rownum rn
FROM (SELECT a.*
FROM activeSessionsLog a
ORDER BY userid asc) b
WHERE rownum <= 10) c
WHERE rn >= 8
Since Oracle 12c you can now use offset/fetch clause.从 Oracle 12c 开始,您现在可以使用 offset/fetch 子句。
You can now write queries like:您现在可以编写如下查询:
SELECT *
FROM user_objects
ORDER object_name
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY
Please review this new feature and also consider the improvements in the execution plan.请查看此新功能并考虑执行计划中的改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.