In an application a have tables projects
, categories
, and a table, that handles the n:m
relationship between of them (project_category_info
):
Now I want to get all projects of a category (I'm solving this with HAVING
). I also needs the information in the result set, which categories each projects belongs to (CONCAT_GROUP
combined with GROUP
):
SELECT
`projects`.*,
`categories`.`id` AS `id`,
GROUP_CONCAT(categories.id SEPARATOR '|||') AS `categories`,
GROUP_CONCAT(categories.short_name SEPARATOR '|||') AS `category_names`
FROM
`projects`
INNER JOIN
`project_category_info` ON `project_category_info`.`project_id` = `projects`.`id`
LEFT JOIN
`categories` ON `project_category_info`.`category_id` = `categories`.`id`
GROUP BY
`projects`.`id`
HAVING
(`categories`.`id` = 3)
;
The result set sontains 13 rows. But when I omit GROUP_CONCAT
and GROUP
, I get one row more. Why? What can cause this behavior?