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 attributes of name X
and name Y
linked to Z
stock. And I wonder if there is any way to do this without doing 2 INNER JOIN
. Currently I do the following form but I believe 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 just and inner join, but if you wanted to know by 5 would you have to do 5 joins? Does anyone know of any simpler way to perform this type of query? There must be the 2 attributes associated with the stock, it is not necessary to have only X
or only Y
associated, there must be 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'