views:

167

answers:

2

This SQL query gives me the results I want; however, I want the results ordered by a different column:

SELECT * 
FROM post 
    INNER JOIN account ON post.account_id = account.account_id 
WHERE post_id > neww 
ORDER BY post_date 
ASC LIMIT 10;

I can not simply change 'ORDER BY post_date ASC' to 'ORDER BY post_id DESC', while that will in fact order the query the way I want it... it will give me the wrong 10 posts.

I simply want to take the EXACT RESULTS of the above query then reorder the results by the post_id.
I would like to do this with SQL if possible, if not I could order the results by adding the results into a new array reversed.

+2  A: 

Use a subquery:

SELECT * FROM (
    SELECT *
    FROM post
    INNER JOIN account
    ON post.account_id = account.account_id
    WHERE post_id > neww
    ORDER BY post_date ASC
    LIMIT 10) AS T1
ORDER BY post_id DESC
Mark Byers
+1 that's what I'd do
Alex
Wow, Notice the race condition that occurred here. (both posted Mar 1 at 22:13) While both answers are correct only one can get the "correct" answer. I find it interesting that Mark's answer names the sub-query `T1` and then doesn't reference it.
sholsinger
+4  A: 

Use a subquery to reorder:

SELECT * FROM (
  SELECT *
  FROM post
  INNER JOIN account ON post.account_id = account.account_id
  WHERE post_id > neww
  ORDER BY post_date ASC LIMIT 10;
) ORDER BY post_id
cletus
Thanks worked like a charm, I cant believe I have been searching 2 days for this :(...
Cody.Stewart
Thanks, I thought that this would be the way to go with my own problem (On SQL Server 2k8). You've confirmed it for me. +1
sholsinger