Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ORDER BY function() 1

Status
Not open for further replies.

donutzeatpeople

Programmer
May 4, 2002
7
US
It seems that whenever I try to make a query with ORDER BY COUNT(field) I always recieve an error from MySQL. Here is the specific query I am trying to use:

SELECT u.username,COUNT(m.entryid) FROM users as u,messages as m WHERE m.userid = u.userid GROUP BY u.userid ORDER BY COUNT(m.entryid) LIMIT 0,10

There must be an easy solution that I just don't see. Can anyone help? Thank you!
 
You can't use an aggregation function in an ORDER BY clause. You can, however, assign an alias to the derived column and use that alias in the ORDER BY clause:

SELECT u.username,COUNT(m.entryid) as thecount[/blue]
FROM users as u, messages as m
WHERE m.userid = u.userid
GROUP BY u.userid
ORDER BY thecount[/blue]
LIMIT 0,10

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top