I would like to create a query that gets a product from the product table, it’s type and category from the type table and the count of songs on the product.
But somehow this query throws an error. It started when I added count(n.name)
SELECT p.name, p.publisher, p.description, p.price, p.picture
, p.releasedate, t.type, t.category, count(n.name) AS songs
FROM Products p
INNER JOIN ProductType t ON (p.type_id = t.id)
INNER JOIN Songs n ON (p.id = n.product_id)
The error I get is
Column ‘Products.name’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Group only
Songsrows, then join the aggregated data instead of theSongstable proper:That way you’ll avoid adding almost all your output columns to the
GROUP BYclause, which you would have to do in the query posted in your question.