reverse order of query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

reverse order of query

Post 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??
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: reverse order of query

Post 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;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: reverse order of query

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
medc
Forum Newbie
Posts: 6
Joined: Mon Mar 02, 2009 3:56 pm

Re: reverse order of query

Post by medc »

I would rather do it from mysql as mintedjo suggested. thanks both of you for your quick reply!!
Post Reply