MySQL query (select statement) question

I have a question about the MySQL select statement. Suppose you have 5 tables: Authors (id, name, email), Categories (id, name), Articles (id, title, summary, article), ArticleAuthors (link between authors and articles) and ArticleCategories (link between article and categories). An article can have more than one author and more than one category. Is it possible to create a select statement that returns all the information (multiple authors and categories) for one article in one row? (no id's for the authors and categories)

Title             Authors             Categories           Summary    Article
-----             -------             ----------           -------    -------
Title of article  Author 1, Author 2  Cat 1, Cat 2, Cat 3  blablabla  Lorum ipsum ...

thanx,

Pannix

3 Replies

Yes, SELECT GROUP_CONCAT(Authors.name), … FROM GROUP BY Articles.id

https://dev.mysql.com/doc/refman/5.5/en … oup-concat">https://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat

Will have to add some more columns, but this is what query looks like for now (and it works):

SELECT a.Title AS Article, 
  GROUP_CONCAT(DISTINCT c.Category ORDER BY c.Category) AS Categories, 
  GROUP_CONCAT(DISTINCT CONCAT(b.FirstName, " ", b.LastName) ORDER BY b.LastName) AS Authors 
FROM Articles AS a 
LEFT JOIN ArticleCategories AS ac ON (a.ArticleID = ac.ArticleID) 
LEFT JOIN Categories AS c ON (ac.CategoryID = c.CategoryID) 
LEFT JOIN ArticleAuthors AS aa ON (a.ArticleID = aa.ArticleID) 
LEFT JOIN Authors AS b ON (aa.AuthorID = b.AuthorID) 
GROUP BY a.ArticleID;

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct