Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Sun Jan 12, 2014 5:29 am 
Offline
Senior Member

Joined: Wed Dec 01, 2010 10:39 am
Posts: 71
Website: http://www.pannix.net
Location: Belgium
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)

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


thanx,

Pannix


Top
   
PostPosted: Sun Jan 12, 2014 7:13 am 
Offline
Senior Member

Joined: Sat Nov 27, 2010 8:21 pm
Posts: 63
Yes, SELECT GROUP_CONCAT(Authors.name), ... FROM <tables> GROUP BY Articles.id

https://dev.mysql.com/doc/refman/5.5/en ... oup-concat


Top
   
PostPosted: Sun Jan 12, 2014 7:25 am 
Offline
Senior Member

Joined: Wed Dec 01, 2010 10:39 am
Posts: 71
Website: http://www.pannix.net
Location: Belgium
Thanks Nibbler


Top
   
PostPosted: Sun Jan 12, 2014 7:55 am 
Offline
Senior Member

Joined: Wed Dec 01, 2010 10:39 am
Posts: 71
Website: http://www.pannix.net
Location: Belgium
Will have to add some more columns, but this is what query looks like for now (and it works):

Code:
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;


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group