Cody.Stewart
Cody.Stewart

Reputation: 577

Running a query with PHP/MySQL then reordering the results by another column

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 > new 
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.

Upvotes: 4

Views: 1916

Answers (2)

cletus
cletus

Reputation: 625147

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

Upvotes: 6

Mark Byers
Mark Byers

Reputation: 838416

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

Upvotes: 4

Related Questions