Page 1 of 1

reverse order of query

Posted: Thu Mar 05, 2009 7:09 am
by medc
I have a mysql query within a php script:
SELECT * FROM table1 WHERE ticker = 'CBU' AND `date` < '2008-01-31' ORDER BY `date` DESC LIMIT 3
If "LIMIT" command weren't used, the query would return about 20 results starting with the most recent date. But since LIMIT is used it returns the 3 most recent dates.

My problem is that I need to use the 3 most recent dates BUT IN REVERSE order.

If I try the "ASC" command in the query, it returns the 3 oldest dates BUT from the total of 20 results, therefore the three dates I need do not appear.

Is there a way to reverse the order of the three rows returned by my query from php??

Re: reverse order of query

Posted: Thu Mar 05, 2009 7:38 am
by mintedjo

Code: Select all

SELECT * FROM (SELECT * FROM table1 WHERE ticker = 'CBU' AND `date` < '2008-01-31' ORDER BY `date` DESC LIMIT 3) as tmp ORDER BY `date` ASC;

Re: reverse order of query

Posted: Thu Mar 05, 2009 10:13 am
by pickle
Depending on your situation, you could use array_unshift() to pull the rows out of the result set & store them in an array.

Re: reverse order of query

Posted: Thu Mar 05, 2009 11:03 am
by medc
I would rather do it from mysql as mintedjo suggested. thanks both of you for your quick reply!!