I have two tables. The name table have a foreign key named ‘categories’. The other table is ‘categories’ that contains name of the categories. The name table references a category in the categories table.
So I use this:
$categories = "SELECT name, COUNT(*) category";
$number_of_categories = "SELECT category, COUNT(1) FROM name GROUP BY category";
As you can see, I’m doing two select statements. I heard that joining tables can do magic, please teach me how to do this in one SELECT statement.
Basically I want to SELECT all the rows from the category table, and then count the number of instances of each category in the name table.
1 Answer