Select with join does not return results

1

I have the following tables

livro
id int pk

passador
id int pk
fk_livro_id int
ordem int

I'm making a book listing screen. The goal is to bring all books that are not registered in the docker

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 passer the search works normally, however when there is no book in the passer the search always returns empty.

    
asked by anonymous 25.01.2018 / 16:48

2 answers

0

I've tailored a query:

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

A subselect is made that brings all fk_livros_id so I compare to the id of the table of books and only display the ones that do not have in the passador table

    
25.01.2018 / 17:07
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
    
29.01.2018 / 20:26