Sådan noget.
Select people.id, people.name, count(interest.id)
from people
left join people_interests on people.id = people_interests.peopleid
left join interests on people_interests.interestid = interests.interest.id
where interests.id in (select id from interests where interests.peopleid = @inputuserid)
group by people.id, people.name
order by count(interest.id)
På engelsk (hvilket måske gør det klarere eller ikke.)
- Vælg personens navn og antallet af interesser, de deler
- Fra persontabellen
- Deltag i interessetabellen, så den tabel
- Er kun interesserne for den person, vi forsøger at matche.
- (grupper efter personer
- og sorter efter antallet af interesser, der matcher.)
Opdateret uden underforespørgsel, men mindre tydelig
Select people.id, people.name, count(interest.id)
from people
left join people_interests on people.id = people_interests.peopleid
left join interests on people_interests.interestid = interests.interest.id
inner join interest i2 on (interests.id = i2.id and i2.people_id = @inputuserid)
group by people.id, people.name
order by count(interest.id)