1

As above, I'm unsure on how to do it, although I am possibly overlooking something simple

I want to retrieve the timestamps and another column, just the most recent 7 which I have done using LIMIT and ordered them using ORDER BY timestamp DESC to get the most recent 7... But once retrieved, I'd like them oldest first rather than newest first

Anyone able to assist please?

Thanks!

user1978592
  • 171
  • 1
  • 1
  • 10
  • One way is to use a subquery to get the 7 you want, then sort those. http://stackoverflow.com/questions/9511882/sorting-by-date-time-in-descending-order http://stackoverflow.com/questions/5912770/select-last-20-order-by-ascending-php-mysql – Douglas Zare Feb 13 '15 at 21:50

2 Answers2

2

two selects could work in this case. it would at least be one possible way to achieve what you want. i'm not sure if it would the best way.

i'm assuming your table has an id field.

select * from records 
  where id in(select id from records order by timestamp desc limit 7) 
  order by timestamp asc;

this lets you get the latest 7 rows in the inner select, then sort them in ascending order.

keune
  • 5,779
  • 4
  • 34
  • 50
  • That brought up an error, but a quick google shows that it just needed an alias adding for the subquery. After adding that, it works perfectly! Thanks! :) – user1978592 Feb 14 '15 at 00:20
0

A nested query should take care of this. Something like

SELECT *
FROM (
    SELECT timestamp, anotherColumn
    FROM tableName
    ORDER BY timestamp DESC
    LIMIT 7 )
ORDER BY timestamp ASC;
Penguinfrank
  • 392
  • 1
  • 9