簡體   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