Link to home
Start Free TrialLog in
Avatar of davideo7
davideo7Flag for United States of America

asked on

MySQL Query: ORDER BY before GROUP BY ?

I have a Query which returns unique users but it doesn't order them how I want, the ORDER BY doesn't seem to even affect the results.  Here's the query:

SELECT user, words
FROM posts
WHERE date > 1325397600
GROUP BY user
ORDER BY words DESC

I want it to select unique unique users but grab the rows for those unique users with the highest words count.  A query like this would be perfect but it doesn't work:

SELECT user, words
FROM posts
WHERE date > 1325397600
ORDER BY words DESC
GROUP BY user
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use a nested query and use order by on the outer one. Should work.
Avatar of davideo7

ASKER

matthewspatrick: That actually worked!  Do you think you could help me with another query where I have a similar situation?

https://www.experts-exchange.com/questions/27516131/MySQL-How-can-I-modify-this-query-to-return-each-user-just-once.html