tail Posted March 16, 2009 Share Posted March 16, 2009 I have a table with a field named "categories". What would be the best way to count all of the records with a certain category? Should I use COUNT for each category or is there a better solution so I don't query the database so many times? Ex: id name category 1 sample cat1 2 sample2 cat2 3 sample3 cat1 4 sample4 cat1 5 sample5 cat2 Result: Cat1: 3 Cat2: 2 Quote Link to comment https://forums.phpfreaks.com/topic/149608-best-way-to-count-records-by-field/ Share on other sites More sharing options...
trq Posted March 16, 2009 Share Posted March 16, 2009 SELECT COUNT(name) as cnt FROM tbl GROUP BY category; Quote Link to comment https://forums.phpfreaks.com/topic/149608-best-way-to-count-records-by-field/#findComment-785624 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.