166

I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working

SELECT 
    *
FROM
    `table`
ORDER BY id DESC
LIMIT 50;

Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either

SELECT 
    *
FROM
    `table`
WHERE
    id > ((SELECT 
            MAX(id)
        FROM
            chat) - 50)
ORDER BY id ASC;

Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?

1
  • I know that this question is old, but please clarify the data structure of the table and, especially, the id column. Is that an integer? is that a string - that can be sorted? That is not 100% clear and is leading to weird answers trying to help. Thanks for your clarification Sep 14 at 10:40

6 Answers 6

303

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.

5
  • 5
    ^ FYI, I know this article is about MySQL, but wanted to advise that in response to @DiegoDD's comment above, omitting the external select and alias does not seem to work in Postgres (9.3.5). I get an 'Error: multiple ORDER BY clauses not allowed'. The accepted answer, however, does work as expected in Postgres
    – allenwlee
    Nov 10, 2014 at 20:56
  • Thanks for this! This query helped me fixed my problem. I have been searching for this for hours
    – Arshad
    May 11, 2015 at 17:41
  • 1
    How can I apply this query with JOINS?
    – GYaN
    Jun 26, 2017 at 8:13
  • @GYaN add joins in the inner sub query Jul 5, 2019 at 20:34
  • What if id is not given then is there a way to do this? Like in oracle we can use rownum fro such situation what can we do in MySQL?
    – Aunny
    Dec 13, 2020 at 17:56
22
SELECT * FROM table ORDER BY id DESC LIMIT 50

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

6
  • 2
    Your query will return the results in reverse (descending id). @newfurniturey's query will get the LAST inserted rows (LIMITing on descending id), and the final result will have ascending ids. Nov 25, 2015 at 7:54
  • 1
    Your solution will not get the results in the right order instead it will get them reverses thus the need for another order by to get them in the right order again
    – Cobra47
    May 26, 2016 at 10:55
  • 3
    But then you just do a php array_reverse() or whatever the equivalent is in your scripting language of choice. The database doesn't need to do that work.
    – Joe
    Aug 7, 2016 at 22:58
  • 8
    @Joe There's nothing in the question that indicates a scripting language is being used, so that's not a safe assumption to make.
    – Nick Coons
    Feb 9, 2018 at 20:01
  • The expected answer should already be sorted using just the database query.
    – d4ryl3
    Feb 20, 2019 at 7:09
11
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!!!!

2

Use it to retrieve last n rows from mysql

Select * from tbl order by id desc limit 10;

use limit according to N value.

1
  • 1
    This is identical to two of the three answers, and the OP's actual query. The question isn't only how to get the last 10 rows but how to get them in ascending order Feb 11, 2022 at 13:48
0

if anyone need this you can change this into

SELECT 
    *
FROM
    `table`
WHERE
    id > ((SELECT 
            MAX(id)
        FROM
            chat) - 50)
ORDER BY id ASC;

into

SELECT 
    *
FROM
    `table`
WHERE
    id > (SELECT MAX(id)- 50 FROM chat) 
ORDER BY id ASC;
-3
select * from Table ORDER BY id LIMIT 30

Notes: * id should be unique. * You can control the numbers of rows returned by replacing the 30 in the query

1
  • 2
    Could you please explain your solution? The explanation in brackets appears to be code, put a whole sentence above/below your code. Thanks
    – deHaar
    Jul 5, 2019 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.