I have so many problems trying to understand this, so forgive me for what’s most likely a repeat question.
I have 2 tables, one called “category” and one called “items”. Each row in “items” has a category_id field that matches the category_id field in the “category” table.
In one mysql statement, I want to get a row count for each category_id in “items”, but also retrieve the category_name in “category”, so I can print out a list of categories like this:
- Foo (3)
- Bar (1)
- Duh (6)
The questions I’ve found here so far only seem to return the category_id’s from “items”, so the result would look like this:
- 1 (3)
- 2 (1)
- 3 (6)
Follow? I’m sure it can be done in one mysql statement, so far I’ve had to resort to two separate queries for the sole purpose of getting that little number.
This should work for you;
//Edit
Sorry I should then explain this.
SelectIs grabbing the category name and the count of the items.FromIs doing a join that will bind items and categories together.Group ByIs ensuring that the Category ID is the unique thing in the query.