Author Topic: Counting in mysql  (Read 279 times)

0 Members and 1 Guest are viewing this topic.

Offline LinuusTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Counting in mysql
« on: February 25, 2010, 04:40:59 AM »
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

Offline straylight

  • Irregular
  • Posts: 19
  • Gender: Male
    • View Profile
Re: Counting in mysql
« Reply #1 on: February 25, 2010, 02:13:07 PM »
This should do it:

Code: [Select]
SELECT parent_id, COUNT(*) count FROM table
GROUP BY parent_id
ORDER BY count DESC
Matt
Avatar Generator - The ultimate forum avatar and banner generation site. Google image search mashup.