jpratt Posted May 20, 2008 Share Posted May 20, 2008 Say i have a table with values like the following: ID: Name: 1 Bob 2 Bob 3 Bob 4 Nick 5 Nick How would i get the value of Bob based off of the fact that it occurs most in the field? Im trying to write a query that selects the three values that occur most in a field. Any ideas? Thanks. Link to comment https://forums.phpfreaks.com/topic/106523-get-most-common-value/ Share on other sites More sharing options...
jonsjava Posted May 20, 2008 Share Posted May 20, 2008 $sql = "SELECT `Name`, COUNT(*) FROM `table` GROUP BY `Name`;"; That should do the trick. Link to comment https://forums.phpfreaks.com/topic/106523-get-most-common-value/#findComment-546089 Share on other sites More sharing options...
spfoonnewb Posted May 20, 2008 Share Posted May 20, 2008 If you only want three values you may want the following: SELECT COUNT(name) AS `total` , `name` FROM `table` GROUP BY `name` ORDER BY `total` DESC LIMIT 3 Link to comment https://forums.phpfreaks.com/topic/106523-get-most-common-value/#findComment-546092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.