Last 5 records MYSQL Sort by ID

how to show the last 5 rows of my MySQL table sorted by ID. For example, I have a table with 15 records. I want to get IDs 10, 11, 12, 13, 14 and 15. In that order. From low to high.

SELECT * FROM temperaturas ORDER BY id DESC LIMIT 5; 

Thus, I get ID 15, 14, 13, 12, 11 and 10. They are the last, but ordered back.

+4
source share
1 answer

This can be done by selecting the last 5 rows, as you did in the internal SELECT, and then reordering it in the external SELECT, i.e.:

 SELECT * FROM (SELECT * FROM temperaturas ORDER BY id DESC LIMIT 5) ORDER BY id; 
+3
source

All Articles