Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last 5 records MYSQL Order by ID

Tags:

mysql

limit

how do I show last 5 rows of my table, MySQL, ordered by ID . For example , I have a table with 15 records. I want to get the ID 10 , 11, 12 , 13, 14 and 15. In that order. Low to High .

SELECT * FROM temperaturas ORDER BY id DESC LIMIT 5;

This way I get ID 15 , 14, 13, 12 , 11 and 10. They are the last , but ordered backwards .

like image 317
safernandez666 Avatar asked Sep 27 '22 23:09

safernandez666


1 Answers

It's doable by selecting the last 5 rows as you've done it in an inner SELECT, and then reordering it in outer SELECT, i.e.:

SELECT * 
FROM (SELECT * FROM temperaturas ORDER BY id DESC LIMIT 5)
ORDER BY id;
like image 87
Alex Shesterov Avatar answered Sep 30 '22 06:09

Alex Shesterov