How to relate several tables?

Asked

Viewed 90 times

1

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

NOTE: I know the basics of SQL, I know you use inner JOIN or can also be done with exists and there is still a third way. The problem is that I only know how to do it with two tables and not like this. Someone can show me what a SELECT to do what I want?

Thank you

Tabelas

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

1

The procedure is the same as with two tables, only add one 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 on SQL Fiddle.

Browser other questions tagged

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