MySQL group by affecting other group
I've got a SUM(value) which calculates the votes for each idea, but this
gets affected by the amount of tags each idea can have.
For example,
SELECT
id,
title,
description,
COALESCE(SUM(case when value > 0 then value end),0) votes_up,
COALESCE(SUM(case when value < 0 then value end),0) votes_down,
GROUP_CONCAT(DISTINCT tags.name) AS 'tags',
FROM ideas
LEFT JOIN votes ON ideas.id = votes.idea_id
LEFT JOIN tags_rel ON ideas.id = tags_rel.idea_id
LEFT JOIN tags ON tags_rel.tag_id = tags.id
GROUP BY ideas.id
So if there are more than one tags.name, then the SUM() get multiplied by
the number of tags.name
How can I fix this?
No comments:
Post a Comment