I'm trying to get some news article from my database and also get the related items with it. The database structure looks like this:
news (id, article )
newsItemsRelation (id, newsId, itemId)
newsAuthorRelation (id, newsId, userId)
items (id, itemTitle )
Then I also have a table for the items and the users with their data.
When I run the query below I get duplicates of the items if I have multiple authors for the same article and I get duplicate authors if I have multiple items for the same article.
SELECT
article,
GROUP_CONCAT(name) AS author,
GROUP_CONCAT(itemTitle) AS item
FROM news
LEFT JOIN newsItemsRelation ON
newsItemsRelation.newsId = news.id
LEFT JOIN items ON
items.id = newsItemsRelation.itemId
LEFT JOIN newsAuthorsRelation ON
newsAuthorsRelation.newsId = news.id
LEFT JOIN profiles ON
profiles.id = newsAuthorsRelation.userId
GROUP BY news.id
EDIT 1
My result look like this:
Article: Article goes here
Author: Walter White, Dexter Morgan, Walter White, Dexter Morgan
Item: Breaking Bad, Dexter, Breaking Bad, Dexter
EDIT 2
Doing GROUP_CONCAT(DISTINCT name) OR GROUP_CONCAT(DISTINCT itemTitle) doesn't solve my problem since multiple items can have the same itemTitle but different ids. Same goes for authors. That means that my result could look like "Dexter, Dexter", though now that looks like "Dexter, Dexter, Dexter, Dexter"
articlein differentid?itemstable structure too?