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

Clinton

Category table:

id, name
---------
1   cat1
2   cat2
3   cat3 
4   cat4

Article table:

id, cid, title
--------------
1   1    title1
2   3    title2
3   1    title3
4   2    title4   

How to select all categories and count number of each category articles in MySQL? I need a mysql query that will select all category and total count of articles belonging to each category and order by category id.

Expected output table:

+-------+-------+    
|  name | count |    
+-------+-------+    
|  cat1 |     2 |    
|  cat2 |     1 |    
|  cat3 |     1 |  
|  cat4 |     0 |  
+-------+-------+
Brijal Savaliya

You can also use subquery like

SELECT c.name,(select count(*) from article where cid = c.id) as count  FROM cat c WHERE 1 GROUP BY c.id 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get all categories on a category page - category.php - when using multiple categories in Wordpress?

From Dev

How to query specific category or all categories inside the same query?

From Dev

How to select the sencond,third record of each category?

From Dev

How to show all sub-categories articles?

From Dev

How to get all the child and grandchild categories of a parent category in codeigniter?

From Dev

Count posts for each category

From Dev

MySQL Select Distinct and count number of each distinct condition matching criteria

From Dev

How get values categories an count products for each category in one query?

From Dev

How can I select all categories in WordPress using a MySQL query?

From Dev

How to select single category in menu if post have two categories?

From Dev

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

From Dev

How to count number of categories in DataFrame object?

From Dev

Select items from distinct categories, including articles with no category

From Dev

How to use GROUP BY to count a new category and old categories at once

From Dev

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

From Dev

How do I select an exact number of articles for each category?

From Dev

How to show all sub-categories articles?

From Dev

Count records in each category

From Dev

Select all categories with topic and post count

From Dev

How can I select all categories in WordPress using a MySQL query?

From Dev

Laravel 5 - Count the number of posts that are assigned to each category in a blog

From Dev

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

From Dev

How to count total number of record for each 7 days in mysql

From Dev

PHP select categories and count number

From Dev

Select all the categories and for each category last 5 post

From Dev

MySQL Select top 10 items for each category

From Dev

PrestaShop: how to get the IDs of the select categories in a Category Treeview?

From Dev

MySQL SELECT from the number of count

From Dev

How to get the number of orders for each category?

Related Related

  1. 1

    How to get all categories on a category page - category.php - when using multiple categories in Wordpress?

  2. 2

    How to query specific category or all categories inside the same query?

  3. 3

    How to select the sencond,third record of each category?

  4. 4

    How to show all sub-categories articles?

  5. 5

    How to get all the child and grandchild categories of a parent category in codeigniter?

  6. 6

    Count posts for each category

  7. 7

    MySQL Select Distinct and count number of each distinct condition matching criteria

  8. 8

    How get values categories an count products for each category in one query?

  9. 9

    How can I select all categories in WordPress using a MySQL query?

  10. 10

    How to select single category in menu if post have two categories?

  11. 11

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

  12. 12

    How to count number of categories in DataFrame object?

  13. 13

    Select items from distinct categories, including articles with no category

  14. 14

    How to use GROUP BY to count a new category and old categories at once

  15. 15

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

  16. 16

    How do I select an exact number of articles for each category?

  17. 17

    How to show all sub-categories articles?

  18. 18

    Count records in each category

  19. 19

    Select all categories with topic and post count

  20. 20

    How can I select all categories in WordPress using a MySQL query?

  21. 21

    Laravel 5 - Count the number of posts that are assigned to each category in a blog

  22. 22

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

  23. 23

    How to count total number of record for each 7 days in mysql

  24. 24

    PHP select categories and count number

  25. 25

    Select all the categories and for each category last 5 post

  26. 26

    MySQL Select top 10 items for each category

  27. 27

    PrestaShop: how to get the IDs of the select categories in a Category Treeview?

  28. 28

    MySQL SELECT from the number of count

  29. 29

    How to get the number of orders for each category?

HotTag

Archive