CanMan2004 Posted December 11, 2006 Share Posted December 11, 2006 Hi allI have a sql table which looks likeID NAME1 Dave2 John3 Dave4 Sarah5 Sarah6 John7 Sarah8 Dave9 Henry10 SarahAs you will noticeSarah is listed 4 timesDave is listed 3 timesJohn is listed 2 timesHenry is listed 1 timeAs im adding to this list, what I want to do is to display the top 3 most popular names, by doing a query which would return the most popular to the least popular but just showing the top 3.The result would look something likeSarah - 4Dave - 3John - 2I use the following query[code]$sql = "SELECT * FROM people";[/code]But I cant figure out how to tweak it to do the above. Can anyone help?Thanks in advanceDave Quote Link to comment Share on other sites More sharing options...
SharkBait Posted December 11, 2006 Share Posted December 11, 2006 [code]SELECT COUNT(*) FROM people GROUP BY name ORDER BY COUNT(*) LIMIT 3[/code]Try that? Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 11, 2006 Author Share Posted December 11, 2006 I get the errorInvalid use of group function Quote Link to comment Share on other sites More sharing options...
trq Posted December 11, 2006 Share Posted December 11, 2006 Assuming the field holding the user names is called [i]username[/i].[code]SELECT username, COUNT(username) AS cnt FROM people GROUP BY username ORDER BY cnt DESC LIMIT 3;[/code] Quote Link to comment Share on other sites More sharing options...
CanMan2004 Posted December 11, 2006 Author Share Posted December 11, 2006 thanks very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.