I need to return the null values of this Inner Join

Asked

Viewed 3,255 times

3

I have this select mysql:

SELECT credor.nome, banco.descricao FROM credor
INNER JOIN banco ON (credor.banco = banco.codigo)

He returns me all the creditors and their respective bank, but if the guy’s bank is NULL he just doesn’t return the record to me.

How can I return the record, but in the description stay NULL?

2 answers

8


What the INNER JOIN is precisely to eliminate the NULLs. It only establishes relationship when there is no NULL at either end.

What you’re wanting is done with LEFT JOIN:

SELECT credor.nome, banco.descricao FROM credor
LEFT JOIN banco ON (credor.banco = banco.codigo)

Here on the site has an excellent explanation about the types of JOIN: What is the difference between INNER JOIN and OUTER JOIN?

0

Try to do as follows below that will return the values NULL:

SELECT credor.nome, banco.descricao FROM credor
  INNER JOIN banco ON (
    if(credor.banco is null, 0, credor.banco) = if(banco.codigo is null, 0, banco.codigo)
  )

Browser other questions tagged

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