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

This SQL query gives me the results I want; however, I want the results to be sorted by another 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't just change ORDER BY post_date ASC to ORDER BY post_id DESC , while it will actually order the request the way I want it ... it will give me the wrong 10 messages.

I just want to take the RESULTS of the RESULTS of the above query, and then change the order of the results to post_id . I would like to do this using SQL, if possible, if I could not order the results by adding the results to a new array.

+4
source share
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 
+6
source

Use 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 
+4
source

Source: https://habr.com/ru/post/1302805/


All Articles