How to select the nth row in a SQL database table?

后端 未结 30 2866
执笔经年
执笔经年 2020-11-22 06:06

I\'m interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can b

30条回答
  •  独厮守ぢ
    2020-11-22 06:27

    SQL 2005 and above has this feature built-in. Use the ROW_NUMBER() function. It is excellent for web-pages with a << Prev and Next >> style browsing:

    Syntax:

    SELECT
        *
    FROM
        (
            SELECT
                ROW_NUMBER () OVER (ORDER BY MyColumnToOrderBy) AS RowNum,
                *
            FROM
                Table_1
        ) sub
    WHERE
        RowNum = 23
    

提交回复
热议问题