我有三个表author,book,author_book。我想要一个mysql查询,它会返回我的作者,书籍列表和显示输出,如下所示:
Author | Books
===========================
Author1 | book1, book2, book3
Author2 | book4
Author3 | book5
更新::
如果我在书表中有作者ID,group_concat为我工作。
但是,如果我有3个表(作者,书籍,作者书),我想显示书名,那么它将如何运作?
答案 0 :(得分:1)
GROUP_CONCAT
是你的朋友,我想一想:
SELECT Author.Name,
GROUP_CONCAT(Books.Name SEPARATOR ', ' ORDER BY Books.Name) AS Books
FROM Author
INNER JOIN Author_Book ON Author.Id = Author_Book.AuthorId
INNER JOIN Book ON Author_Book.BookId = Book.Id
WHERE Author.Id=Books.AuthorId
GROUP BY Author.Name
ORDER BY Author.Name