Vivid Lust Posted June 5, 2011 Share Posted June 5, 2011 I'm trying to get the top 2 most frequent values in a specific column in a db, so heres an example column_name 7 8 8 4 5 7 7 8 8 RESULT: 8,7 Is this possible? And if so how can it be achieved? Thanks lots!! Jake Quote Link to comment https://forums.phpfreaks.com/topic/238480-top-2-most-frequent-values-in-column/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 5, 2011 Share Posted June 5, 2011 SELECT column_name FROM your_table GROUP BY column_name ORDER BY COUNT(*) DESC LIMIT 2 Quote Link to comment https://forums.phpfreaks.com/topic/238480-top-2-most-frequent-values-in-column/#findComment-1225450 Share on other sites More sharing options...
Pikachu2000 Posted June 5, 2011 Share Posted June 5, 2011 This should get you the results you're after. SELECT COUNT(field) FROM table GROUP BY field ORDER BY COUNT(field) DESC LIMIT 2 Quote Link to comment https://forums.phpfreaks.com/topic/238480-top-2-most-frequent-values-in-column/#findComment-1225451 Share on other sites More sharing options...
ManiacDan Posted June 5, 2011 Share Posted June 5, 2011 PFMaBiSmAd's solution gives you the field name, Pikachu's solution gives you the two highest frequencies. The former is probably what you're after. If you want the value AND the frequency, just combine the two: SELECT field, COUNT(field) as frequency FROM table GROUP BY field ORDER BY COUNT(field) DESC LIMIT 2 -Dan Quote Link to comment https://forums.phpfreaks.com/topic/238480-top-2-most-frequent-values-in-column/#findComment-1225481 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.