简体   繁体   English

在oracle中使用rownum获取数据

[英]fetching data using rownum in oracle

I have a query in oracle to fetch data from table using rownum but i didn't get any data. 我在oracle中有一个查询,要使用rownum从表中获取数据,但没有得到任何数据。
My query is like this : 我的查询是这样的:

select * from table-name where rownum<5

is this is a wrong query to fetch data whose row number is less than 5. 这是获取行号小于5的数据的错误查询吗?
when i used query like : 当我使用像这样的查询时:
select * from table-name where rownum<=4
than it will gives a result record. 比它会给出结果记录。

My question is what is wrong here with ? 我的问题是这里出什么问题了?
Is this is syntax error or anything else??.. 这是语法错误还是其他??

rownum is a pseudo column that counts rows in the result set after the where clause has been applied. rownum是伪列,该伪列在应用where子句之后对结果集中的行进行计数。

  SELECT table_name
    FROM user_tables
    WHERE rownum > 2;
TABLE_NAME                     
------------------------------

0 rows selected

However, this query will always return zero rows, regardless of the number of rows in the table. 但是,此查询将始终返回零行,而不管表中的行数如何。

To explain this behaviour, we need to understand how Oracle processes ROWNUM. 为了解释这种行为,我们需要了解Oracle如何处理ROWNUM。 When assigning ROWNUM to a row, Oracle starts at 1 and only increments the value when a row is selected; 将ROWNUM分配给行时,Oracle从1开始,并且仅在选择行时才递增该值; that is, when all conditions in the WHERE clause are met. 也就是说,当满足WHERE子句中的所有条件时。 Since our condition requires that ROWNUM is greater than 2, no rows are selected and ROWNUM is never incremented beyond 1. 由于我们的条件要求ROWNUM大于2,所以不会选择任何行,并且ROWNUM永远不会超过1。

http://blog.lishman.com/2008/03/rownum.html http://blog.lishman.com/2008/03/rownum.html

another stackoverflow link 另一个stackoverflow链接

Edited 已编辑

this paragraph i find on oracle website which is much better 我在oracle网站上找到了这个段落

Conditions testing for ROWNUM values greater than a positive integer are always false. ROWNUM值大于正整数的条件测试始终为false。 For example, this query returns no rows: 例如,此查询不返回任何行:

SELECT * FROM employees
    WHERE ROWNUM > 1;

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. 提取的第一行的ROWNUM分配为1,并使条件为false。 The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. 现在要获取的第二行是第一行,并且还分配了ROWNUM为1并使条件为false。 All rows subsequently fail to satisfy the condition, so no rows are returned. 随后所有行均不满足该条件,因此不返回任何行。

You can also use ROWNUM to assign unique values to each row of a table, as in this example: 您还可以使用ROWNUM为表的每一行分配唯一值,如本示例所示:

Syntax seems correct to me. 语法对我来说似乎是正确的。

However ROWNUM is calculated on result rows for example: 但是,ROWNUM是根据结果行计算的,例如:

SELECT * FROM TABLE_NAME WHERE ROWNUM < 10 ORDER BY TABLE_FIELD ASC;

and

SELECT * FROM TABLE_NAME WHERE ROWNUM < 10 ORDER BY TABLE_FIELD DESC;

Will give you different results. 会给你不同的结果。

Each time a query is executed, for each tuple, Oracle assigns a ROWNUM which is scopet to that only query. 每次执行查询时,Oracle会为每个元组分配一个ROWNUM,该行的作用域是该唯一查询。

What are you trying to accomplish ? 你想达到什么目的 ?

Since for the reasons rahularyansharma mentions, rownum based queries won't always function the way you might expect, a way around this is to do something like 由于出于rahularyansharma提及的原因,基于行数的查询并不总是能按您期望的方式运行,因此解决此问题的方法是执行类似

SELECT * from (SELECT rownum AS rn, TABLE_NAME.* FROM TABLE_NAME)
where rn > 5;

However, be aware that this will be a fairly inefficient operation when operating over large datasets. 但是,请注意,在对大型数据集进行操作时,这将是一个非常低效的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM