Getting the count of rows in each category in a MySQL table

Boopathy

Let me say, I have two tables category and questions. In questions table I have few or more questions under the same category and the table has questions of various categories.

I need the count of rows in each category under the question table.

Category: +----+--------+--+ | id | name | | +----+--------+--+ | 1 | Type 1 | | | 2 | Type 2 | | | 3 | Type 3 | | +----+--------+--+

Questions +----+------+-------+ | id | name | catid | +----+------+-------+ | 1 | a | 1 | | 2 | b | 1 | | 3 | c | 1 | | 4 | d | 2 | | 5 | e | 2 | | 6 | f | 3 | +----+------+-------+

category_count = [3,2,1]

What I did to get the count is,

if($model){
        $i = 0; $j = 0;
        $category_count = 0;
        $count [] = null;

        foreach($model as $question) {
            $category_count++;
            if(isset($output))
                if($question->catid != $output[$i-1]['cat'] ){
                    $count[$j] = $category_count;
                    $j++;
                    $category_count = 0;
                }
            $output[$i++] = ['id' => $question->id, 'cat' => $question->catid, 'title' => $question->title];
        }
        $count[$j]=$category_count;
        $final = ['count'=>$count, 'questions'=>$output];
}

My Question is,

  1. Instead of doing it in code can we get the count array using sql query efficiently.
  2. If not, help me in optimizing the code.

T!A.

marijnz0r

Depending on the DDL of your tables, your query should look like this:

SELECT c.name, COUNT(*) 
FROM category c
JOIN questions q
ON q.catid = c.id
GROUP BY c.name

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting the count of rows in each category in a MySQL table

From Dev

Sql getting count of each category

From Dev

Sql getting count of each category

From Dev

mysql sum and count per category append to matching category rows

From Dev

SQL Getting count of distinct rows for each value

From Dev

Mysql getting rows by each month in each year

From Dev

MySQL getting count of a related table

From Dev

Mysql join table and count rows

From Dev

Count posts for each category

From Dev

Count records in each category

From Dev

Count posts for each category

From Dev

MySQL - Count number of rows in each group

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

MySQL - getting a count from the same table

From Dev

Mysql Count of rows not referenced in other table

From Dev

How to count certain rows in a MySQL table?

From Dev

MySQL - Join & Count rows from another table

From Dev

Mysql Count of rows not referenced in other table

From Dev

How to count some rows of a MySQL table

From Dev

MySQL - Count all rows for each day of week but for each sender

From Dev

display last 2 entries in each category from a mysql table

From Dev

display last 2 entries in each category from a mysql table

From Dev

MySQL insert category rows

From Dev

MySQL Not getting rows with zero from count and outer join

From Dev

Getting the count of rows with the same

From Dev

Trying to get MySql order by count, but only getting 1 of each

From Dev

How can I count rows matching criteria for each week in MySQL?

From Dev

Getting status by N last rows from each group in MySQL

Related Related

HotTag

Archive