Will Barangi Will Barangi - 1 month ago 6
MySQL Question

Subquery returns more than one row error result from mysql

I have a question, I am trying to go through a data base and display all the books by an author based on a search where I use an author name to get isbn and then find all the details of that isbn...well if the author wrote one book it is displaying one row but when the author wrote more than one book it is giving me an error...what am I doing wrong can you please help...Here is my code.

SELECT* FROM books WHERE isbn=(SELECT isbn FROM books_authors
WHERE author_id IN
(SELECT author_id FROM authors WHERE first_name ="J.K."))

Answer

Change to:

SELECT* FROM books WHERE isbn in (SELECT isbn FROM books_authors 
  WHERE author_id IN 
 (SELECT author_id FROM authors WHERE first_name ="J.K."))

You can't have isbn=(subquery) if the subquery returns multiple results.