Listing all categories with the total number of products

0

I need to mount a query that displays the names of all categories, and in front of each name the total number of products.

The tables are:

 Categorias (cat_codigo, cat_nome)
 Produtos (pro_codigo, pro_codigo_categoria pro_nome)      

 Exemplo:

 Parafusos (58)
 Brocas (42)
 Chaves (33)

I can make the links between the two tables, but I am in doubt about getting the total amount:

 select * from categorias CAT LEFT JOIN produtos PRO ON CAT.cat_codigo=PRO.pro_codigo_categoria
    
asked by anonymous 08.05.2018 / 14:27

1 answer

1
 select  count(PRO.pro_nome), CAT.cat_nome
 from categorias CAT
 INNER JOIN 
   produtos PRO ON CAT.cat_codigo=PRO.pro_codigo_categoria
 group by CAT.cat_nome

It will list the number of products for each category.

    
08.05.2018 / 14:55