Alex
Alex

Reputation: 713

Count all different record relation with single MySQL query

I have a mysql table subArticles, which includes sub articles and has a column named _topRecord inside, which holds a number to specify the top article.

Also,each article can have many sub-articles, so the subArticles table may have several records where _topRecord = 10.

Now I want to count how many subArticles every article owns. I can do it this way when I know the specific Article ID:

SELECT Count(*) 
  FROM subArticles 
 WHERE _topRecord = 2;

But for 80 articles I don't want to replay this query for 80 times.

How can it be done in an SQL Query solution?

Upvotes: 0

Views: 25

Answers (1)

Jens
Jens

Reputation: 69440

Is this what you need:

  SELECT COUNT(*),_topRecord 
    FROM subArticles 
GROUP BY _topRecord?

Upvotes: 2

Related Questions