Fosfor
Fosfor

Reputation: 441

Double order by on a query in mysql

that I want to get is from the table posts the 8 most rated posts(posts.rates) and the 8 most viewed(posts.views) on the same query, so I get 16 results at all. Both fields are on the same table. So it's late and actually can't figure how to do it. Thanks in advance.

I wan't do do something like below in one query, getting 16 rows:

SELECT id FROM posts
ORDER BY rates DESC
LIMIT 0,8;


SELECT id FROM posts 
ORDER BY views DESC
LIMIT 0,8;

EDIT: Answer:

SELECT id FROM (SELECT id FROM posts ORDER BY views DESC LIMIT 0,8) AS a
UNION ALL
SELECT id FROM (SELECT id FROM posts ORDER BY rates DESC LIMIT 0,8) AS b

Upvotes: 1

Views: 648

Answers (1)

peterm
peterm

Reputation: 92785

Try UNION

(SELECT id FROM posts ORDER BY rates DESC LIMIT 0,8)
UNION ALL
(SELECT id FROM posts ORDER BY views DESC LIMIT 0,8)

Upvotes: 1

Related Questions