我有一个带有发布表的数据库,每个发布可以有多个作者存储在不同的表中.我想查询数据库,在一列中给出一个出版物标题列表,在第二列中给出该出版物的合并作者.
SELECT p.`id`, p.`title`, a.`fullname`
from `publications` p
LEFT JOIN `authors` a on a.`publication_id` = p.`id`;
Run Code Online (Sandbox Code Playgroud)
这当然给了我许多作者多次出版的标题.
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
Run Code Online (Sandbox Code Playgroud)
对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 …Run Code Online (Sandbox Code Playgroud)