Linuus Posted February 25, 2010 Share Posted February 25, 2010 Hi! I'm using Joomla and I need to create a query that counts all rows (articles) in my table that have the same "parent_id" (category). I can do it like this: SELECT COUNT(*) FROM table WHERE parent_id = $my_id and then loop that for every parent_id. There might be many parent_ids so I wonder if I somehow can count everything in one query and then get the data from an array with a loop. Like use the GROUP by parent_id COUNT(*) as c .... I hope someone understands what I mean. If not, scream and I will try to explain better Mysql version: 5.0.67 Best regards Linus Link to comment https://forums.phpfreaks.com/topic/193325-counting-in-mysql/ Share on other sites More sharing options...
straylight Posted February 25, 2010 Share Posted February 25, 2010 This should do it: SELECT parent_id, COUNT(*) count FROM table GROUP BY parent_id ORDER BY count DESC Link to comment https://forums.phpfreaks.com/topic/193325-counting-in-mysql/#findComment-1018156 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.