rownum issue in oracle query

China☆狼群 提交于 2019-12-08 08:26:52

问题


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:

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.


回答1:


Please use the thread below. Pretty good explanations. https://community.oracle.com/thread/210143 Here are two working solutions.

  1. Use the row_number function for 8i and 9i :
    ROW_NUMBER() OVER (ORDER BY ASC) AS 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



回答2:


You need to apply ROWNUM after Oracle figures out which rows to return. 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;



回答3:


Change "and rownum < 200" to "WHERE rownum < 200"



来源:https://stackoverflow.com/questions/15590215/rownum-issue-in-oracle-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!