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 Link to comment https://forums.phpfreaks.com/topic/30176-resolved-php-count-top-3/ 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? Link to comment https://forums.phpfreaks.com/topic/30176-resolved-php-count-top-3/#findComment-138731 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 Link to comment https://forums.phpfreaks.com/topic/30176-resolved-php-count-top-3/#findComment-138740 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] Link to comment https://forums.phpfreaks.com/topic/30176-resolved-php-count-top-3/#findComment-138742 Share on other sites More sharing options...
CanMan2004 Posted December 11, 2006 Author Share Posted December 11, 2006 thanks very much Link to comment https://forums.phpfreaks.com/topic/30176-resolved-php-count-top-3/#findComment-138746 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.