How to make a query with different table conditions

Asked

Viewed 455 times

2

TABLE REGISTER A
Attributes: Code, name, type
TABLE NUMBER B
Attributes: Code, number
TABLE PERSON C
Attributes: Code, gender

I need to bring the information name and type of table A with the corresponding numbers of table B filtering by the attributes "type" of table A and "gender" of table C.

Follow my attempt to consult:

select A.NOME, B.NUMERO
from A
left join B on
A.codigo = B.codigo
where
A.tipo=B and
C.genero='Homem'
  • 4

    Where is the link of "C.genero" with tables A and B? Column "A.tipo" is a foreign key for B? It seems to me that you need to improve the modeling of tables. Post more information about the business rule that we can help you with.

1 answer

2

The modeling needs to improve even, but for what I saw code is primary and foreign key. Assuming this is true, then it is necessary to include the PERSON table in the query in order to search for a field of it. Your query could look like this:

SELECT c.nome, n.numero, p.genero
FROM cadastro c
LEFT JOIN numero n ON c.codigo = n.codigo
LEFT JOIN pessoa p ON c.codigo = p.codigo
WHERE c.tipo='sei la' and p.genero='Homem';

Browser other questions tagged

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