Reputation: 196
I have this two tables:
ID category_name
-----------------------
1 Pizza
2 Salad
3 Desert
4 Sandwiches
ID name category
--------------------------------
1 Margherita 1
2 Quatro Stagioni 1
3 Pancakes 3
4 Some Salad 2
I want to make a SQL statement which will get the name of the category and the number of products from each category.
Upvotes: 1
Views: 30
Reputation: 311073
You can join the two tables and group the result by the category:
SELECT category_name, COUNT(*)
FROM categories c
JOIN products p ON p.category = c.id
GROUP BY category_name
Upvotes: 1