0

我正在尝试将我拥有的书籍的主题放在我的网站上。我在 MySqli 中有一个查询可以完美地为我提供信息,除了大多数书籍的 isbn 编号列出了多个主题。这是我的查询:

SELECT b.id,b.title, sss.title as subject 
from books b
LEFT JOIN book_subjects bs on b.id = bs.book
LEFT JOIN sub_subject ss on ss.id = bs.sub_subject
LEFT JOIN sub_sub_subject sss on bs.sub_sub_subject = sss.id
where b.isbn13 = 9780596515898

和结果集:

489282  JavaScript: The Missing Manual (Missing Manual Series)  Network Programming
489282  JavaScript: The Missing Manual (Missing Manual Series)  Programming Languages
489282  JavaScript: The Missing Manual (Missing Manual Series)  Web Programming / Development

我如何才能让所有科目(网络编程、编程语言等)都在一个领域而不是三个领域?我希望它看起来像:

489282  JavaScript: The Missing Manual (Missing Manual Series) Network Programming, Programming Language, Web Programming/ Developement

我可以尝试在查询中执行此操作,也可以在生成结果后使用 Php 执行此操作。我确实尝试过

CONCAT(sss.title) as subject

但这在我的查询中引发了错误。我不确定 foreach 循环是否有效,所以我想我会在花费数小时徒劳尝试之前寻求帮助。

4

1 回答 1

1

试试GROUP_CONCAT

SELECT b.id,b.title, GROUP_CONCAT(sss.title) as subject 
from books b
LEFT JOIN book_subjects bs on b.id = bs.book
LEFT JOIN sub_subject ss on ss.id = bs.sub_subject
LEFT JOIN sub_sub_subject sss on bs.sub_sub_subject = sss.id
where b.isbn13 = 9780596515898
于 2013-10-21T14:09:12.953 回答