How do I make an INNER JOIN conditional?

Asked

Viewed 467 times

0

I have the following SQL that displays me only properties that have a registered photo, as specified in the lines:

INNER JOIN fotos
    ON imoveis.id = fotos.cod

How to display real estate with or without registered photos?

Follow the full SQL

SELECT
    imoveis.id, fotos.foto, imoveis.titulo, imoveis.descricao, imoveis.vvenda, imoveis.vtemporada, imoveis.vanual, imoveis.status, imoveis.cod
    FROM
    imoveis
    INNER JOIN fotos
    ON imoveis.id = fotos.cod
    INNER JOIN tipo
    ON imoveis.tipo = tipo.cod
    INNER JOIN bairros
    ON imoveis.bairro = bairros.cd_bairro
WHERE imoveis.cod = '1015' AND imoveis.status = '1' GROUP BY imoveis.id ORDER BY imoveis.id DESC
  • As Rafael Araújo indicates you should use LEFT JOIN. In stackoverflow there is a lot of information on this topic. <br> Example.:<br> https://answall.com/questions/6441/qual-%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join

1 answer

3


You must use LEFT JOIN for the photo tables.

SELECT imoveis.id, fotos.foto, imoveis.titulo, imoveis.descricao, imoveis.vvenda, imoveis.vtemporada, imoveis.vanual, imoveis.status, imoveis.cod
FROM
imoveis
INNER JOIN tipo
ON imoveis.tipo = tipo.cod
INNER JOIN bairros
ON imoveis.bairro = bairros.cd_bairro
LEFT JOIN fotos
ON imoveis.id = fotos.cod
WHERE imoveis.cod = '1015' AND imoveis.status = '1' GROUP BY imoveis.id ORDER BY imoveis.id DESC

Browser other questions tagged

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