Returning the last N results

You want to skip directly to the last page of your results.

Solution

Calculate how many rows there are in the full results-set and use an appropriate OFFSET or do a reverse-sort ref.

Discussion

There is no way to directly return the last page of a results set, but you can try:

SELECT count(*) FROM companies;
   

And take the result from that (N say) to calculate an offset of N-P in your OFFSET (see [return-next-n]). Remember that the last page might only be half full though, so you want N mod P not N-P if you are being strict about these things.

Alternatively, you can do:

SELECT * FROM companies ORDER BY co_id DESC LIMIT 5;
   

This gives you the first five results but has sorted them in a reverse order. If you can't be bothered to reverse them in your application you can use a view (or a temporary table).

CREATE VIEW tmp_co_view AS SELECT * FROM companies
ORDER BY co_id DESC LIMIT 5;
SELECT * FROM tmp_co_view ORDER BY co_id;
   

This grabs the page we want in reverse order then re-sorts it. Don't forget to drop the view when you finish with it.