我有两个通过ID相关的表。

图书:

book_id | book_name | year | publisher | slug


章节:

chapter_id | book_id | chapter_name


现在,当我查询数据库以查找与这样的书相关的所有章节时:

SELECT book_name, chapter_name FROM ci_books, ci_chapters WHERE ci_books.book_id = ci_chapters.book_id AND slug = 'twelvers-dawn';


我得到:

**book_name | chapter_name**
twelvers  | Rising Sun
twelvers  | Slow Down
twelvers  | Masochist


我得到的书名在每一章中都重复出现,如何阻止该名字重复出现,从而只显示一次名称,并显示所有章节?

最佳答案

GROUP_CONCAT()GROUP_BY一起使用

SELECT book_name, GROUP_CONCAT(chapter_name) AS chapter_names
FROM ci_books, ci_chapters
WHERE ci_books.book_id = ci_chapters.book_id AND slug = 'twelvers-dawn'
GROUP BY book_name;

关于mysql - 在mysql中停止列值的重复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19897362/

10-12 14:03