Select with Join does not return results

Asked

Viewed 66 times

1

I have the following tables


livro
id int pk

passador
id int pk
fk_livro_id int
ordem int

I am making a screen listing books. The goal is to bring all the books that are not registered in the dealer


I made the following query:

SELECT * FROM livro as l JOIN passador as p ON l.id != p.fk_livro_id AND like %?%

When I have at least one book registered in the dealer the search works normally, however when there is no book in the dealer the search always returns empty.

2 answers

0

An alternative solution using LEFT JOIN

SELECT 
   * 
FROM livro 
LEFT JOIN passador
   ON livro.id = passador.fk_livro_id
WHERE passador.fk_livro_id IS NULL

0


I adapted a query:

select * from livro as l 
where l.id not in (select fk_livro_id from passador)

It is made a subselect that brings all the fk_livros_id therefore, I compare with the id table of books and display only those that do not have in the table passador

  • The result is the same, when there is not at least one book registered in the dealer the search does not return results, however if there is 1 works perfectly

  • @Adrianoluz checks again, I edited the select.

  • Exactly what I was looking for. I would just like to apply the same logic using Join

Browser other questions tagged

You are not signed in. Login or sign up in order to post.