I'm having trouble displaying database records in the manner I want using PHP and Mysql.
I have 3 tables, BOOKS
, AUTHORS
, BOOKS_AUTHORS
.
The BOOKS
table has the following columns, book_id
, title
, publish_date
, thumbnail
. The AUTHORS
table has the following columns, author_id
, first_name
, last_name
. The BOOKS_AUTHORS
table has the following columns, books_authors_id
, book_id
(linked to book_id
in the BOOKS
table), and author_id
(linked to author_id
in the AUTHORS
table).
Here's the SQL I'm using to display the results.
SELECT books.book_id, title, publish_date, thumbnail, authors.author_id, first_name, last_name, books_authors.book_id, books_authors.author_id
FROM books, authors, books_authors
WHERE books.book_id = books_authors.book_id
AND authors.author_id = books_authors.author_id
AND books.book_id = (INSERT NUMBER HERE)
Everything works fine unless a book has more than 1 author. Let's say a book has 2 authors. I will get back 2 records that have the same title, publish_date, and thumbnail but each will have a different authors' name. So now I have 2 duplicate titles, 2 duplicate publish_dates, and 2 duplicate titles. This isn't how I want to display the records. I want both author names to appear under 1 title, 1 publish_date, and 1 thumbnail.