A join will create a record for each combination, so let's say you have 2 categories and 3 items in each category, so you would get 6 results.
Example:
Code:
Category_ID Name Item_ID Item Name Price
----------------------------------------------------------------------------------------------------------------------------------
1 Category 1 1 Item 1 5.00
1 Category 1 2 Item 2 6.00
1 Category 1 3 Item 3 7.00
2 Category 2 4 Item 4 8.00
2 Category 2 5 Item 5 9.00
2 Category 2 6 Item 6 10.00
The group by says, I want my results grouped by X, in this case category_id and name, so it will group the results by those columns
Example:
Code:
Category_ID Name
-----------------------------------------------------------------------------------------------------------------
1 Category 1
----------------------------------------------------------------------
Item_ID Item Name Price
----------------------------------------------------------------------
1 Item 1 5.00
2 Item 2 6.00
3 Item 3 7.00
2 Category 2
----------------------------------------------------------------------
Item_ID Item Name Price
----------------------------------------------------------------------
4 Item 4 8.00
5 Item 5 9.00
6 Item 6 10.00
With group by, you can only select the columns you group on. But you can add new columns so long as they are a result of COUNT(), SUM(), AVG(), MIN(), MAX(), etc.
I used COUNT(item_id), so that it would count the items in the category group, thus providing you with the category id, name and the number of items in that category.
Thus you end up with
Code:
Category_ID Name item_count
----------------------------------------------------------------------------------------------------------------------------------
1 Category 1 3
2 Category 2 3
Bookmarks