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

后端 未结 30 2845
执笔经年
执笔经年 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:52

    PostgreSQL supports windowing functions as defined by the SQL standard, but they're awkward, so most people use (the non-standard) LIMIT / OFFSET:

    SELECT
        *
    FROM
        mytable
    ORDER BY
        somefield
    LIMIT 1 OFFSET 20;
    

    This example selects the 21st row. OFFSET 20 is telling Postgres to skip the first 20 records. If you don't specify an ORDER BY clause, there's no guarantee which record you will get back, which is rarely useful.

提交回复
热议问题