MySQL: combining multiple values after a join into one result column

浪尽此生 提交于 2019-12-04 01:43:41

You need to group by all of the non-aggregated columns in the SELECT columns (and explicitly, not the author id, because author.id is not part of the select list):

SELECT p.`id`, p.`title`, GROUP_CONCAT(a.`fullname` separator ', ')
from `publications` p 
LEFT JOIN `authors` a on a.`publication_id` = p.`id` 
GROUP BY p.`id`, p.`title`;

Stuart's answer is fine. This is just to show the working version of your approach:

SELECT p.`id`, p.`title`, a.`fullname`
FROM `publications` p LEFT JOIN
      (SELECT publication_id, GROUP_CONCAT(a.`fullname` separator ', ')
       FROM `authors` a
       GROUP BY publication_id
      ) a
--------^
      ON a.`publication_id` = p.`id`;

The error you got was because the a was missing after the subquery. The subquery also needed to be fixed, to include publication_id in the select and a group by clause.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!