INNER JOIN with 3 tables

Asked

Viewed 76,391 times

7

I have 3 tables

Tb_contratocotista:

inserir a descrição da imagem aqui

Tb_contract:

inserir a descrição da imagem aqui

Tb_stakeholder:

inserir a descrição da imagem aqui



I would like to relate the values of the table Tb_contract with Tb_stakeholder, but the way I got it only returns the result of Tb_contract. What am I missing ?

SELECT * FROM TB_Contrato
INNER JOIN (SELECT id_contrato FROM TB_Cotista
            INNER JOIN TB_ContratoCotista
            ON TB_Cotista.id_cotista = TB_ContratoCotista.id_cotista) AS VAI
ON TB_Contrato.id_contrato= VAI.id_contrato

1 answer

29


Just do 2 INNER JOIN by listing the keys of the tables.

SELECT * FROM TB_ContratoCotista
INNER JOIN TB_Contrato ON TB_Contrato.id_contrato = TB_ContratoCotista.id_contrato
INNER JOIN TB_Cotista ON TB_Cotista = TB_ContratoCotista.id_cotista

You can define which fields appear in select:

Returning the columns of all tables

select * from...

Returning columns only from the Tb_contract table

select TB_Contrato.* from...
  • Perfect, I was trying to make it easier than I imagined.

Browser other questions tagged

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