Skip to content
Advertisement

SQL query is only returning one entry, when it should be returning several

I am creating a book tagging system and I am trying to call all the books with the same tag. My query is only turning up the first book, and not any of the following books with the same tag.

Here is the books table for the database

Here is the book_tags table for the database:

Here is the BookTagDAOImpl file which calls to the query:

Advertisement

Answer

As a starter: the if statement should probably be a while loop instead.

However, this is also a problem with your query:

This is missing a join condition between the two tables, so this basically returns all books as long as at least one of them has the searched tag. Presumably, you want:

You might as well use EXISTS:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement