Select specific row from mysql table

前端 未结 6 587
忘掉有多难
忘掉有多难 2020-12-07 23:08

Ideally I need a query that is equivalent to

select * from customer where row_number() = 3

but that\'s illegal.

I can\'t u

6条回答
  •  孤城傲影
    2020-12-07 23:27

    SQL tables are not ordered by default, and asking for the n-th row from a non ordered set of rows has no meaning as it could potentially return a different row each time unless you specify an ORDER BY:

    select * from customer order by id where row_number() = 3
    

    (sometimes MySQL tables are shown with an internal order but you cannot rely on this behaviour). Then you can use LIMIT offset, row_count, with a 0-based offset so row number 3 becomes offset 2:

    select * from customer order by id
    limit 2, 1
    

    or you can use LIMIT row_count OFFSET offset:

    select * from customer order by id
    limit 1 offset 2
    

提交回复
热议问题