How to select a row based on its row number?

前端 未结 6 1486
猫巷女王i
猫巷女王i 2020-12-28 18:12

I\'m working on a small project in which I\'ll need to select a record from a temporary table based on the actual row number of the record.

How can I select a record

6条回答
  •  猫巷女王i
    2020-12-28 18:43

    A couple of the other answers touched on the problem, but this might explain. There really isn't an order implied in SQL (set theory). So to refer to the "fifth row" requires you to introduce the concept

    Select *
    From 
    (
        Select 
          Row_Number() Over (Order By SomeField) As RowNum
        , *
        From TheTable
    ) t2
    Where RowNum = 5
    

    In the subquery, a row number is "created" by defining the order you expect. Now the outer query is able to pull the fifth entry out of that ordered set.

提交回复
热议问题