简体   繁体   English

MySQL:将连接后的多个值组合成一个结果列

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

I have a database with a table for publications, each of which can have multiple authors that are stored in a different table.我有一个包含出版物表的数据库,每个出版物都可以有多个作者,这些作者存储在不同的表中。 I'd like to query the database into giving me a list of publication titles in one column, and the combined authors for that 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 multiple times the publication title for as many authors.这当然给了我多次作者的出版物标题。

id   title              fullname
--   -----              --------
1    Beneath the Skin   Sean French
1    Beneath the Skin   Nicci Gerrard
2    The Talisman       Stephen King
2    The Talisman       Peter Straub

Grouping on id gives me one author per title:按 id 分组给我每个标题一个作者:

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
--   -----              --------
1    Beneath the Skin   Sean French
2    The Talisman       Stephen King

The result I'm looking for is this:我正在寻找的结果是这样的:

id   title              fullname
--   -----              --------
1    Beneath the Skin   Sean French, Nicci Gerrard
2    The Talisman       Stephen King, Peter Straub

I think the answer should be found in using GROUP_CONCAT, but the only result I'm able to get is one result row with all authors:我认为答案应该在使用 GROUP_CONCAT 中找到,但我能得到的唯一结果是所有作者的一个结果行:

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
--   -----              --------
1    Beneath the Skin   Sean French, Nicci Gerrard, Stephen King, Peter Straub

And using GROUP_CONCAT after the join gives me an "Every derived table must have its own alias" error.在连接后使用 GROUP_CONCAT 会给我一个“每个派生表都必须有自己的别名”错误。

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?有什么线索吗?

You need to group by all of the non-aggregated columns in the SELECT (and explicitly, not group by the author id, because author is part of the GROUP_CONCAT):您需要按 SELECT 中的所有非聚合列进行分组(并且明确地,不是按作者 ID 分组,因为作者是 GROUP_CONCAT 的一部分):

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.您得到的错误是因为在子查询之后缺少a The subquery also needed to be fixed, to include publication_id in the select and a group by clause.还需要修复子查询,以在selectgroup by子句中包含publication_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM