I have 5 tables being those:
my_books
table:
- id_book
- title
- price
- description
genre
table:
- id_genre
- genre(genre description in this column)
genre_ind
table:
- id_book
- id_genre(table combines genre and books tables)
authors
table:
- id_author
- author(author name in this column)
author_ind
table:
I need to join tables to select all books info by genre id. Since author info stored on other table I need to add authors info according to book id.
Trying to do that in a single query and so far I have the following query:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.authors
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON books.id_book=author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author=s.id_author
WHERE author_ind.id_book=my_books.id_book
AND genre_ind.id_genre=%d
I have functional query if I need to select info from my_books info based on the genre id but then I try to extend it to select author info and it does not work!
Update:
now code quire looks like this:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.authors
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON my_books.id_book = author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author = s.id_author
WHERE genre_ind.id_genre=%d
and the error I get is: MySQL: #1054 - Unknown column 's.authors' in 'field list'
Thank you for your help, really appreciate it
Ok, problem solved. Thank you so much for your time and help!!!!
correct quire:
SELECT my_books.id_book,
my_books.title,
my_books.price,
my_books.descrip,
a.genre,
s.author
FROM my_books
LEFT JOIN genre_ind
ON my_books.id_book = genre_ind.id_book
LEFT JOIN genre a
ON genre_ind.id_genre = a.id_genre
LEFT JOIN author_ind
ON my_books.id_book = author_ind.id_book
LEFT JOIN authors s
ON author_ind.id_author = s.id_author
WHERE genre_ind.id_genre=%d