Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 764
Cody.Stewart Avatar asked Dec 29 '22 00:12

Cody.Stewart


2 Answers

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
like image 122
cletus Avatar answered Dec 31 '22 12:12

cletus


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
like image 30
Mark Byers Avatar answered Dec 31 '22 14:12

Mark Byers