Inner Join is returning repeated results

0

I have three tables! One of them being a relationship table!

They are:

Person:

Treatment:

RelationshipbetweenPersonandTreatmenttable:

I would like to pass the ID of the person, the query would return the NAMES of the selected treatments. Example: ID 8, he would return Reiki and Astrology; ID 9, he would return Reiki, Astrology and Personal Development Coaching!

What's happening in my query:

I've tried the query by putting Distinct , but it's not working! It's like he's returning two results (which I hope are two results even) but that's all coming from the other treatments as well.

My code snippet:

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

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

    
asked by anonymous 16.04.2018 / 03:24

1 answer

2

The table keys (FK of the treatment_person with the treatment PK) were missing from JOIN, as @Rovann commented it 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
    
16.04.2018 / 03:45