PDA

View Full Version : MySQL - (Select Order Limit ) THEN Order again ASC?



TehGuy
June 15th, 2011, 02:27
The title basically explains the problem I'm having. I'm pretty new to SQL, so hopefully there's an easy way to do this.

My query looks like this right now:


$result = mysql_query("SELECT * FROM table ORDER BY date DESC LIMIT 15");

It selects the biggest 15 dates from the table, sorted in descending order. After I get the 15 biggest dates, I want to re-sort them in ascending order. I'm not sure how to do that really. I could always make a temporary table, copy the data to it, then select from THAT table and sort it the way I want, but that seems hugely inefficient to me. I also thought about a 2-dimensional array then sorting the array by the date, but I'm hoping there's an easy way to do this with MySQL.

Any ideas how to select a limited number of the biggest values from a table, then sort it?

jkl6
June 28th, 2011, 15:02
Wrap another Select around the query, something like this:
$result = mysql_query("SELECT * FROM (SELECT * FROM table ORDER BY date DESC LIMIT 15) ORDER BY date ASC");

JJW
June 29th, 2011, 09:46
$result = mysql_query("SELECT * FROM table ORDER BY 'id' DESC, ORDER BY 'DATE' ASC LIMIT 15");

Try that.

iBrightDev
June 29th, 2011, 10:57
use MAX



$result = mysql_query("SELECT *, MAX(date) FROM table ORDER BY date ASC LIMIT 15");