Select last N rows from MySQL

SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50

If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.

That's what worked for me!!!! Hope it will help!!!!


You can do it with a sub-query:

SELECT * FROM
(
 SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;

This will select the last 50 rows from table, and then order them in ascending order.


SELECT * FROM table ORDER BY id DESC LIMIT 50

save resources make one query, there is no need to make nested queries


Use it to retrieve last n rows from mysql

Select * from tbl order by id desc limit 10;

use limit according to N value.

Tags:

Mysql

Database