Hi guys. I've hit a brick wall with this one and I'm sure it's a really simple answer.

I have a table from which I want the last 50 records sorted by ASC - however doing a SELECT * from table order by id ASC limit 50 shows the first (not last) 50 records.

Running SELECT * from table order by id DESC gives the records that I want but in the wrong order. Basically I want:

50th to last record
49th to last record
...
...
Second to last record
Last record

As I said - I'm sure it's something really simple and I'm not seeing the forest for the trees. I have a feeling some form of sub-query will be needed.

Thanks for your thoughts.

Jamie

    select * 
       from (select * 
                   from t
                  order by id desc
                  limit 50) dt
     order by id 
    

      The last line ought to be

      ORDER BY dt.id

        Or of course you could fetch the first fifty records in descending order into an array and then reverse the array....

          6 days later

          Thanks guys that worked perfectly 🙂

            Write a Reply...