sql count by value stack by different value

i have my database like this:

table1(article)
id | name | description | cat_id
0 | val1 |   desc1     |   1
1 | val2 |   desc2     |   2
2 | val3 |   desc3     |   1
3 | val4 |   desc4     |   2
4 | val5 |   desc5     |   1
5 | val6 |   desc6     |   2
6 | val7 |   desc7     |   0


table2(categories)
id | name 
0  | shoe
1  | glove
2  | hat

it should return:

articles_connected | categorie_name
        3          |  glove
        3          |  shoe
        0          |  hat

i tried:

SELECT 
    COUNT(Category.id) as article_amount, Category.name
FROM 
    article_table as Article,
    category_table as Category
WHERE
    Category.id =  Article.cat_id

this code just counts all as one, doesn’t split up my categories. I’m not sure what goes wrong.


Source: sql

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.