Question

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:

  • id_book
  • id_author

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
Was it helpful?

Solution

The first problem is this on clause:

ON books.id_book=author_ind.id_book

Nothing called books has been defined.

A second problem is that the where clause is undoing the effect of the left outer join.

Perhaps you mean this:

 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
---------^ changed from `books`
      authors s
      ON author_ind.id_author=s.id_author
WHERE genre_ind.id_genre=%d
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top