开发者

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

开发者 https://www.devze.com 2022-12-21 11:11 出处:网络
This SQL query gives me the results I want; however, I want the results ordered by a different 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 RESUL开发者_如何学JAVATS 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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号