Inner Join is returning repeated results

Asked

Viewed 245 times

0

I have three tables! One of them is a relation table!

Are they:

Person:inserir a descrição da imagem aqui

Treating:inserir a descrição da imagem aqui

Relationship Table between Person and Treatment:inserir a descrição da imagem aqui

I would like that when passing the person’s ID, the query returns the NAMES of the selected treatments. Example: ID 8, he would return Reiki and Astrology; ID 9, he would return Reiki, Astrology and Coaching Personal Development!

What’s happening in my consultation:inserir a descrição da imagem aqui

I already tried to make the consultation putting Distinct, but it’s not working! It’s like he’s returning two results(what I hope are two results same) only that’s coming all other treatments too.

My snippet of code:

Select * FROM tratamento INNER JOIN pessoa_tratamento_r ON 8 = pessoa_tratamento_r.ID_PESSOA

Thanks in advance for the help! And please, if you want to put links or other indications for study, do this! If there are other ways to do this research as well, please say!

  • 1

    select t.nome from tratamento t inner join pessoa_tratamento pt on pt.id_tratamento = t.id_tratamento and pt.id_pessoa = 8

1 answer

2


Missing to relate the keys of the tables (FK of the people_treatment with the treatment PK) in JOIN, as @Rovann commented would look like this:

select t.nome 
from tratamento t 
     inner join pessoa_tratamento pt on pt.id_tratamento = t.id_tratamento 
where pt.id_pessoa = 8

Browser other questions tagged

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