simonp Posted November 22, 2007 Share Posted November 22, 2007 Hi, I have a table with lots of entries, eg: Bob Fred Bob Graham Bob Harold Bob Frank William Fred Bob How can I find out which is the most popular name (ie which one appears the most often - in this example 'Bob')? Thanks Simon Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/ Share on other sites More sharing options...
GingerRobot Posted November 22, 2007 Share Posted November 22, 2007 How about: SELECT COUNT(*) AS `num`,`name` FROM `yourtable` GROUP BY(`name`) ORDER BY `num` DESC If you do just want the most popular, rather than an ordered listing of popularity, stick a LIMIT clause on the end: SELECT COUNT(*) AS `num`,`name` FROM `yourtable` GROUP BY(`name`) ORDER BY `num` DESC LIMIT 1 Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/#findComment-397132 Share on other sites More sharing options...
simonp Posted November 23, 2007 Author Share Posted November 23, 2007 Hi -thanks for that. I'm struggling to get the results - I'm using: $sql2 = "SELECT COUNT(*) AS 'num','victim' FROM 'victims' GROUP BY('victim') ORDER BY 'num' DESC LIMIT 1"; @$result2=mysql_query($sql2,$db); How do I get the most popular name now?! Cheers Simon Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/#findComment-397443 Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 You don't use single quotes for starters. You use single quotes to enclose strings within you query. The character i used was the backtick (`) - which you can use to surround table names, field names etc. You don't have to do this, but i think it looks cleaner and easier to read. Anyway, Try: <?php $sql2 = "SELECT COUNT(*) AS `num`,`victim` FROM `victims` GROUP BY(`victim`) ORDER BY `num` DESC LIMIT 1"; @$result2=mysql_query($sql2,$db); if($result2){ $row = mysql_fetch_assoc($result2); echo 'The most popular victim is: '.$row['victim'].' who has been the victim on '.$row['num'].' occassions'; } ?> Not quite sure why being the victim makes you popular, but yeah! Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/#findComment-397461 Share on other sites More sharing options...
simonp Posted November 23, 2007 Author Share Posted November 23, 2007 GingerRobot - that's brilliant! Works perfectly. Thanks! Simon Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/#findComment-397554 Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 No problem, glad to help. Quote Link to comment https://forums.phpfreaks.com/topic/78478-solved-most-popular-record-in-a-table/#findComment-397575 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.