例如,在我的数据库中,我有 3 个表,
- 用于存储书籍数据的 Books 表
- 用于存储标签数据的标签表
- 以及用于存储关于具有多个标签的书的信息的链接表(Book_Tags 表)。
如下图所示...(箭头上方的“M”表示关系的多方面)
BOOKS BOOK_TAGS TAGS
+----+---------+ +---------+----------+ +----+---------------+
|ID | Title | |Book_Id | Tags_Id | |ID | Tag_Name |
+----+---------+ +---------+----------+ +----+---------------+
|1 | Book_1 | 1 M |1 | 1 | M 1 |1 | Tag_Name_1 |
|1 | Book_1 |<--------|1 | 2 |-------->|2 | Tag_Name_2 |
+----+---------+ |1 | 3 | |3 | Tag_Name_3 |
|2 | 1 | +----+---------------+
|2 | 3 |
+---------+----------+
我的问题是,我如何查询我的数据库,以便得到类似的结果
+---------------------------------------------------------+
|ID | Book_Title | Tags |
+---------------------------------------------------------+
|1 | Book_1 | Tag_Name_1, Tag_Name_2, Tag_Name_3|
|2 | Book_2 | Tag_Name_1, Tag_Name_3 |
+---------------------------------------------------------+
而不是像
+----------------------------------+
|ID | Book_Title | Tags |
+----------------------------------+
|1 | Book_1 | Tag_Name_1 |
|2 | Book_1 | Tag_Name_2 |
|3 | Book_1 | Tag_Name_3 |
|4 | Book_2 | Tag_Name_1 |
|5 | Book_2 | Tag_Name_3 |
+----------------------------------+