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. Quote Link to comment 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. Quote Link to comment 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 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.