1
I have the following tables:
Tabela: estoque
id_estoque
nome
Tabela: atributos
id_atributo
id_estoque
nome
And for example I need to know if there are 2 name attributes X
and the name Y
stocked Z
. And I wonder if there’s any way to do that without doing 2 INNER JOIN
. Currently I do the following way but I believe that there must be some simpler way that I do not know:
SELECT e.nome
FROM estoque e
INNER JOIN atributos a1 ON e.id_estoque = a1.id_estoque AND a1.nome = 'X'
INNER JOIN atributos a2 ON e.id_estoque = a2.id_estoque AND a2.nome = 'Y'
WHERE e.id_estoque = 'Z'
Because to know with 2 are only and Inner Join, but if I wanted to know for 5 would have to do 5 joins? Does anyone know any simpler way to perform this type of query? There must be the 2 attributes associated with the stock X
or just Y
associated, there must be the 2.
Data example:
Tabela: estoque
id_estoque|nome
'A'|'A'
'B'|'B'
'C'|'C'
'Z'|'Z'
Tabela: atributos
id_atributo|id_estoque|nome
'X'|'A'|'X'
'Y'|'A'|'Y'
'N'|'A'|'N'
'X'|'Z'|'X'
'Y'|'Z'|'Y'
'M'|'Z'|'M'
I believe it would work using
WHERE a.nome like '%X' OR a.nome like '%Y'
– MarceloBoni
does not serve because it is not to be or but to have the 2 attributes obligatorily associated to the stock
– Leonardo Patricio