Fórum Utilização da condição WHERE dentro de um INNER JOIN #605157
05/09/2019
0
coluna1 coluna2 coluna3
1 a 0
2 b 0
3 c 0
1 d 1
2 e 1
3 f 1
1 g 2
2 h 2
3 i 2
Pretendo obter este resultado:
coluna1 coluna2 coluna3 coluna1 coluna2 coluna3
1 a 0 1 d 1
2 b 0 2 e 1
3 c 0 3 f 1
A consulta que escrevi é essa:
select * from teste as a where coluna3 = '0' INNER JOIN ( select * from teste where coluna3 = '1' ) as b ON a.coluna1 = b.coluna1
Obtendo este erro:
ERROR: syntax error at or near "INNER"
LINE 4: INNER JOIN (
^
SQL state: 42601
Character: 47Tirando a condição where = '0' da primeira tabela, a consulta funciona, mas não me retorna o resultado esperado.
A pergunta é: não é possível usar a condição WHERE com INNER JOIN? Se sim, como? O que está errado nessa sintaxe?
André Carreiro
Curtir tópico
+ 0Post mais votado
06/09/2019
select *
from (select * from teste
where coluna3 = '0') a
full outer join (select * from teste
where coluna3 = '1') b on (a.coluna1 = b.coluna1)Tainan Ramos
Gostei + 1
Mais Posts
06/09/2019
Jair N.
select * from teste as a
INNER JOIN (
select * from teste
where coluna3 = ''1''
) as b
ON a.coluna1 = b.coluna1
where a.coluna3 = ''0''
Gostei + 1
06/09/2019
André Carreiro
select * from teste as a
INNER JOIN (
select * from teste
where coluna3 = ''1''
) as b
ON a.coluna1 = b.coluna1
where a.coluna3 = ''0''
Obrigado! Era justamente essa regra de sintaxe que eu precisava compreender.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)