Clarification on “rownum”

前端 未结 3 518
慢半拍i
慢半拍i 2021-01-25 19:27

I have a table Table1


Name     Date
A 01-jun-2010
B 03-dec-2010
C 12-may-2010

When i query this table with the

3条回答
  •  生来不讨喜
    2021-01-25 20:05

    (Not an Oracle expert by any means)

    From what I understand, rownum numbers the rows in a result set.

    So, in your example:

    select * from table1 where rownum=2
    

    How many rows are there going to be in the result set? Therefore, what rownum would be assigned to such a row? Can you see now why no result is actually returned?

    In general, you should avoid relying on rownum, or any features that imply an order to results. Try to think about working with the entire set of results.

    With that being said, I believe the following would work:

    select * from (select rownum as rn,table1.* from table1) as t where t.rn = 2
    

    Because in that case, you're numbering the rows within the subquery.

提交回复
热议问题