How do I limit the number of rows returned by an Oracle query after ordering?

前端 未结 17 1489
夕颜
夕颜 2020-11-21 04:56

Is there a way to make an Oracle query behave like it contains a MySQL limit clause?

In MySQL, I can do this:

         


        
17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 05:25

    For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.

      SELECT * FROM sometable1 so
        WHERE so.id IN (
        SELECT so2.id from sometable2 so2
        WHERE ROWNUM <=5
        )
        AND ORDER BY so.somefield AND ROWNUM <= 100 
    

    I have implemented this in oracle server 11.2.0.1.0

提交回复
热议问题