I have a table name BRANDS. I have another table named MODELS. The tables are like this:
BRANDS
BrandID // pk
BrandName
MODELS
ModelID // pk
BrandID // fk
ModelName
Each brand has zero to many brands associated with it. Each model can be associated with only one brand.
My output should look something like this:
Brand # of Models
Goodyear 5
Chevy 19
Toyota 7
The query I use to get the models looks like this:
SELECT BrandID, BrandName
FROM Brands
ORDER BY BrandName
My count query looks like this:
SELECT COUNT(BrandID) AS TotalBrands
FROM Brands
WHERE SiteID = ?
I have a few other things that I need to count also, from other tables.
Can you get me started on how to count the items from the MODELS table?
Thanks!!!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ANSER
I used all of your answers as hints to help me get what I needed. This is the real query.
SELECT S.SiteID,
S.SiteName,
COUNT(BrandID) AS TotalBrands
FROM Sites S
LEFT JOIN Brands B
ON S.SiteID = B.SiteID
GROUP BY S.SiteID, S.SiteName
1 Answer