You need to create a MySQL query: "Select in the reverse order m records from the database, starting with n records".

Posted by:

 SELECT * FROM items WHERE id > $pos ORDER BY id DESC LIMIT 5 

But I think that it is not worth writing this way, and there is a more correct form of request.

  • Does the query work as intended? What exactly does not suit you in the request? - Regent
  • If there are a lot of records, then this is not optimal, as we will have to go through the whole table until we get to the required id - dudka649
  • Add an index (if it is not already) to the id field. If you need to select the first five lines, the id which is greater than a certain value and which have the maximum id among all the records found, then this query should be. - Regent
  • If you need to sort the data by descending id and take 5 records from 10 places, and WHERE id > $pos used as a crutch for this, you can change the query for SELECT * FROM items ORDER BY id DESC LIMIT 9, 5 - Regent

1 answer 1

If the database involves the removal of rows, then such a selection is meaningless. If the lines are not deleted, the request should be of the following form:

 SELECT * FROM items WHERE id >= n AND id <= n+m ORDER BY id DESC LIMIT m