简体   繁体   English

oracle查询中的rownum问题

[英]rownum issue in oracle query

I m trying to run this query but it returns zero rows. 我正在尝试运行此查询,但它返回零行。 Any clues why? 有什么线索吗?

Select distinct a.id
from  table1 a, table b
where  ( a.id= b.id or a.id = b.secondid ) and rownum < 200;

But if I run the above query without the ROWNUM clause it finds records: 但是,如果我在没有ROWNUM子句的情况下运行上述查询,它将找到记录:

Select distinct a.id
from  table1 a, table b
where  ( a.id= b.id or a.id = b.secondid );

I'm confused why the first query is not working. 我很困惑为什么第一个查询不起作用。

You need to apply ROWNUM after Oracle figures out which rows to return. Oracle确定要返回哪些行后,需要应用ROWNUM The only reliable way is this: 唯一可靠的方法是这样的:

SELECT * FROM (
  Select distinct a.id
  from  table1 a, table b
  where  ( a.id= b.id or a.id = b.secondid )
) WHERE ROWNUM < 200;

Please use the thread below. 请使用下面的线程。 Pretty good explanations. 很好的解释。 https://community.oracle.com/thread/210143 Here are two working solutions. https://community.oracle.com/thread/210143这是两个可行的解决方案。

  1. Use the row_number function for 8i and 9i : 对8i和9i使用row_number函数:
    ROW_NUMBER() OVER (ORDER BY ASC) AS ROW_NUMBER. ROW_NUMBER()超过(按ASC排序)为ROW_NUMBER。
  2. Reuse Rownum pseudo column (This seems to be better) SELECT * FROM ( SELECT t.*, ROWNUM AS rn FROM ( SELECT * FROM mytable ORDER BY paginator, id ) t ) WHERE rn BETWEEN 1 AND 3 重用Rownum伪列(这似乎更好)SELECT * FROM(SELECT t。*,ROWNUM AS rn FROM(SELECT * FROM mytable ORDER BY paginator,id)t)在1和3之间

将“ and rownum <200”更改为“ WHERE rownum <200”

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

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