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

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

    For SQL Server, a generic way to go by row number is as such:

    SET ROWCOUNT @row --@row = the row number you wish to work on.
    

    For Example:

    set rowcount 20   --sets row to 20th row
    
    select meat, cheese from dbo.sandwich --select columns from table at 20th row
    
    set rowcount 0   --sets rowcount back to all rows
    

    This will return the 20th row's information. Be sure to put in the rowcount 0 afterward.

    0 讨论(0)
  • 2020-11-22 06:50

    ADD:

    LIMIT n,1
    

    That will limit the results to one result starting at result n.

    0 讨论(0)
  • 2020-11-22 06:50
    SELECT * FROM emp a
    WHERE  n = (SELECT COUNT( _rowid)
                  FROM emp b
                 WHERE a. _rowid >= b. _rowid);
    
    0 讨论(0)
  • 2020-11-22 06:51

    I'm not sure about any of the rest, but I know SQLite and MySQL don't have any "default" row ordering. In those two dialects, at least, the following snippet grabs the 15th entry from the_table, sorting by the date/time it was added:

    SELECT * FROM the_table ORDER BY added DESC LIMIT 1,15
    

    (of course, you'd need to have an added DATETIME field, and set it to the date/time that entry was added...)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 06:53

    It seems to me that, to be efficient, you need to 1) generate a random number between 0 and one less than the number of database records, and 2) be able to select the row at that position. Unfortunately, different databases have different random number generators and different ways to select a row at a position in a result set - usually you specify how many rows to skip and how many rows you want, but it's done differently for different databases. Here is something that works for me in SQLite:

    select * 
    from Table 
    limit abs(random()) % (select count(*) from Words), 1;
    

    It does depend on being able to use a subquery in the limit clause (which in SQLite is LIMIT <recs to skip>,<recs to take>) Selecting the number of records in a table should be particularly efficient, being part of the database's meta data, but that depends on the database's implementation. Also, I don't know if the query will actually build the result set before retrieving the Nth record, but I would hope that it doesn't need to. Note that I'm not specifying an "order by" clause. It might be better to "order by" something like the primary key, which will have an index - getting the Nth record from an index might be faster if the database can't get the Nth record from the database itself without building the result set.

    0 讨论(0)
提交回复
热议问题