I have a database with a table for publications, each of which can have several authors, which are stored in another table. I would like to query the database, showing me a list of publication names in one column and the combined authors for this publication in the second.
SELECT p.`id`, p.`title`, a.`fullname` from `publications` p LEFT JOIN `authors` a on a.`publication_id` = p.`id`;
This, of course, gives me several times the title of the publication for many authors.
id title fullname
Grouping by ID gives me one author per title:
SELECT p.`id`, p.`title`, a.`fullname` from `publications` p LEFT JOIN `authors` a on a.`publication_id` = p.`id` GROUP BY a.`id`; id title fullname
As a result, I am looking for:
id title fullname
I think the answer should be found when using GROUP_CONCAT, but the only result I can get is a single line of results with all authors:
SELECT p.`id`, p.`title`, GROUP_CONCAT(a.`fullname`) from `publications` p LEFT JOIN `authors` a on a.`publication_id` = p.`id` GROUP BY a.`id`; id title fullname
And using GROUP_CONCAT after the connection, I get the message "Each view must have its own alias."
SELECT p.`id`, p.`title`, a.`fullname` FROM `publications` p LEFT JOIN (SELECT GROUP_CONCAT(a.`fullname`) FROM `authors` a) ON a.`publication_id` = p.`id`;
Any clues?