0

Here is an situation that probably has an easy solution but I just can't figure it out...

I have a analytics dashboard that shows the metrics in a line chart based on year, quarter (1-4)

I need to grab the last 8 newest records in a database..simple, order by year,quarter limit 0,8. BUT I need to display it left to right with the oldest year, first quarter.. So actually in reverse, not newest to oldest. So I need something like:

2011 - Quarter 2
2011 - Quarter 3
2011 - Quarter 4
2012 - Quarter 1
2012 - Quarter 2
2012 - Quarter 3
2012 - Quarter 4
2013 - Quarter 1 (Which is the last added record)

I was just going to query it based on the year DESC, quarter ASC then just reverse the output. Was going to use a for loop and set the $i to 8, $i > 0, $i-- then just increment from last record to first. Problem is I have no idea how to get the row number.

So in short, I need to query newest to oldest, but output oldest to newest. So, this is not an SQL Query question, an output question using mysql.

1
  • No idea I was supposed to do that and I dont see where that is at. Oct 19, 2012 at 17:36

2 Answers 2

1

Use a subquery, something like:

SELECT * FROM Revenue WHERE id IN 
  (SELECT id FROM Revenue WHERE ... youc criteria ... ORDER BY ... criteria order ...) 
  ORDER BY ... display order ...
1
  • Here is the answer: $results = mysql_query($strQuery) or die(mysql_error()); while ($row = array_reverse(mysql_fetch_array($results))) { echo $row['something']; } Oct 22, 2012 at 12:47
0

Here is the answer:

$results = mysql_query($strQuery) or die(mysql_error()); 

while ($row = array_reverse(mysql_fetch_array($results))) { 

echo $row['something']; 

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.