[英]Combining the many side of a many-to-one relationship into one field?
两个表: books
和authors
。 一本书可以有很多作者,就像短篇小说选集一样。
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
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,如果它是依赖于系统的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.