I'm a newbie with sql and I've managed to select rows with relationship to many tables with the sql below. I now have a forth table with items that are "children" to the model. There is a relation between "id" in the models table (526 rows) and "model_id" in the items table (1505 rows). I would now want to count the items in the items table that have the same model_id as the id in the models table and output them as Items_in_model.
SELECT models.id AS Id, categories.name AS Category, models.category_name
AS CategoryName, manufacturers.name AS Manufacturer, models.model AS Model,
models.day_price AS dayprice, models.week_price AS weekprice
FROM
models, categories, manufacturers
WHERE models.category=categories.id AND
models.manufacturer=manufacturers.id
ORDER BY CategoryName ASC
I got the counting working "standalone" with this sql
SELECT COUNT(items.id) AS Count FROM items WHERE items.model_id
LIKE 2
I've tried many different approaches but this is the only one that didn't give me an error but only outputs one row (of 526) and the count is (1505 as in all items)
SELECT models.id AS Id, categories.name AS Category,
models.category_name AS CategoryName, COUNT(items.id) AS Count,
manufacturers.name AS Manufacturer, models.model AS Model,
COUNT(DISTINCT(items.id)) AS ModelCount, models.day_price AS dayprice,
models.week_price AS weekprice
FROM
models, categories, manufacturers, items
WHERE models.category=categories.id AND
models.manufacturer=manufacturers.id AND models.id=items.model_id
ORDER BY CategoryName ASC
Thanks in advance!