INNER JOIN with 3 tables

4

I have 3 tables

TB_ContratoCotista:



TB_Contract:



TB_Cotist:



I would like to relate the values of the TB_Contract table with TB_Cotist but the way I got it just returns the result of the 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
    
asked by anonymous 03.07.2015 / 19:25

1 answer

17

Just do 2 INNER JOIN relating table keys.

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 the select:

Returning the columns of all tables

select * from...

Returning columns only from the TB_Contract table

select TB_Contrato.* from...
    
03.07.2015 / 19:27