繁体   English   中英

将多对一关系的多方面结合到一个领域?

[英]Combining the many side of a many-to-one relationship into one field?

两个表: booksauthors 一本书可以有很多作者,就像短篇小说选集一样。

图书

id    |    title
--------------------
1        The Time Machine Did It
2        Snakes in Suits
3        Ghost in the Wires

作者

id    |    name
------------------------
1        Stephen King
2        John Swartzwelder
3        Robert D. Hare
4        Kevin Mitnick
5        William L. Simon
6        Steve Wozniak
7        Paul Babiak

books_authors_link

book_id    |    authors_id
---------------------------
1                2
2                3
2                7
3                4
3                5
3                6

我想做的是返回一个表,其中每一行都是一本书,'authors'字段是所有作者的逗号分隔列表,例如

查询结果

title                    |    authors
---------------------------------
The Time Machine Did It    John Swartzwelder
Snakes in Suits            Robert D. Hare, Paul Babiak
Ghost in the Wires         Kevin Mitnick, William L. Simon, Steve Wozniak

我一直在玩GROUP BY和连接函数,我知道答案就在那里,但我无法弄明白。 我可以通过在程序本身中处理数组和字符串连接来实现它,但这不是我正在寻找的优雅解决方案。 这里的提示将不胜感激。

我正在使用SQLite 3,如果它是依赖于系统的。

您必须逐个连接两个表,并在作者名称group_concat上应用group_concat。

该功能的官方文档在这里

这是我在stackoverflow中找到的另一个例子

暂无
暂无

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

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