2
Exemplifying, I have the
tabela_A
:
cod nome
1 stack
2 overflow
3 stackoverflow
and tabela_B
:
cod_tabela_A ano mes valor
1 2016 1 100
1 2016 2 115
2 2016 1 90
When made a LEFT JOIN, returns me the following, correctly:
SELECT * FROM tabela_A a
LEFT JOIN tabela_B b ON a.cod = b.cod_tabela_A
cod nome cod_tabela_A ano mes valor
1 stack 1 2016 1 100
1 stack 1 2016 2 100
2 overflow 2 2016 1 90
3 stackoverflow NULL NULL NULL NULL
If I add the WHERE clause:
SELECT * FROM tabela_A a
LEFT JOIN tabela_B b ON a.cod = b.cod_tabela_A
WHERE ano = 2016 AND mes = 2
Returns me a single record, correctly:
cod nome cod_tabela_A ano mes valor
1 stack 1 2016 2 100
But what I need is that when I do not obey the values indicated in WHERE, return me NULL:
cod nome cod_tabela_A ano mes valor
1 stack NULL NULL NULL NULL
1 stack 1 2016 2 NULL
2 overflow NULL NULL NULL NULL
3 stackoverflow NULL NULL NULL NULL
Is there any way to get this result?
Remove the
WHERE
and let aloneand b.ano = 2016 AND b.mes = 2
– Marco Souza
Where is because it has other conditions inside it, but following what was said, the result came out as expected.
– Marcelo de Andrade
I did not understand the negative vote...
– Marcelo de Andrade