How to relate multiple tables?

0

Below I put an example that I did in excel. How does a select change the values of the inf_musicas table by the value of the other corresponding tables?

NOTE: I know the basics of SQL, I know it uses inner JOIN or it can also be done with exists and it still has a third way. The problem is that I only know how to do it with two tables and not like that. Can anyone show me what a SELECT query would look like to do what I want?

Thank you

    
asked by anonymous 15.02.2018 / 18:19

2 answers

1

I think what you want is this:

SELECT
    m.id_inf_mus AS id,
    b.nome_banda AS banda,
    g.nome_grava AS gravadora,
    q.quant_cd AS cds
FROM inf_musicas m
INNER JOIN banda b ON m.id_banda = b.id_banda
INNER JOIN gravadora g ON m.id_grava = g.id_grava
INNER JOIN quanti q ON m.id_qnt_cds = q.id_qnt_cds
    
15.02.2018 / 18:31
1

The procedure is the same as with two tables, just add a join to the other tables:

SELECT 
  X.ID_INF_MUS, 
  A.NOME_BANDA, 
  B.NOME_GRAVA, 
  C.QUANT_CD 
FROM INF_MUSICAS X
INNER JOIN BANDA A ON X.ID_BANDA = A.ID_BANDA
INNER JOIN GRAVADORA B ON X.ID_GRAVA = B.ID_GRAVA
INNER JOIN QUANTI C ON X.ID_QNT_CDS = C.ID_QNT_CDS

See working in SQL Fiddle .

    
15.02.2018 / 18:34