select the TOP N rows from a table

后端 未结 5 1731
谎友^
谎友^ 2020-12-13 06:35

I am making some paging, and I need to make some query and get the result form defined slicing . for example: I need to get all \"top\" rows in range 20n < x < 40n et

5条回答
  •  感动是毒
    2020-12-13 07:04

    Assuming your page size is 20 record, and you wanna get page number 2, here is how you would do it:

    SQL Server, Oracle:

    SELECT *   -- <-- pick any columns here from your table, if you wanna exclude the RowNumber
    FROM (SELECT ROW_NUMBER OVER(ORDER BY ID DESC) RowNumber, * 
          FROM Reflow  
          WHERE ReflowProcessID = somenumber) t
    WHERE RowNumber >= 20 AND RowNumber <= 40    
    

    MySQL:

    SELECT * 
    FROM Reflow  
    WHERE ReflowProcessID = somenumber
    ORDER BY ID DESC
    LIMIT 20 OFFSET 20
    

提交回复
热议问题