I have 3 tables, resuming them:
:::NEWS:::
ID | TITLE
1 | New A
2 | New B
:::TAGTONEW:::
NEWID | TAGID
1 | 1
1 | 2
2 | 2
2 | 3
:::TAGS:::
ID | TAG
1 | religion
2 | sport
3 | politic
And the goal of this is to search news that match a given list of tags (e.g. News with the tags sport and religion). Ok. The problem is that when I do something like:
SELECT * FROM news JOIN tagtonew ON news.id = newid JOIN tags ON tagid = tags.id
WHERE tag IN ('religion','sport');
The result will be 3 rows, 2 of them repeating the New A
. Ok, I can use GROUP BY news.id
to get just one row, but the problem is that I need to return ALL the tags that match with New A (I need to return that New A is related to sport and religion), how can I do that? Requering the newid? Thanks in advance.
edit
The one above will result:
New A - religion
New A - sport
New B - sport
If I group will be like:
New A - religion
New B - sport
But I NEED to know that A was also related to sport, because I need to return it in a JSON. So I need the output to be something like:
New A - (religion, sport)
New B - sport