In Postgres 12, I'm trying to perform a SELECT
on the recipes
table bringing only one image (files). However, when performing the query without GROUP BY
I get duplicate recipes according to the number of its images (files). When trying to use GROUP BY
, I get the following error:
column "f.path" must appear in the GROUP BY clause or be used in an aggregate function
The query I'm running is:
SELECT r.id, r.title, c.name AS chef_name, f.path
FROM recipes AS r
LEFT JOIN chefs AS c ON (r.chef_id = c.id)
LEFT JOIN recipe_files AS rf ON (rf.recipe_id = r.id)
LEFT JOIN files AS f ON (rf.file_id = f.id)
GROUP BY r.id, c.id
ORDER BY r.title ASC
If I add f.path
to GROUP BY
, I return to the initial problem of receiving the listing with duplicate items according to the number of images (files).