对我而言很复杂,因为我是SQL的新手。
我有三个表格 - Peoples
,Interests
和Peoples_Interests
(多对多) - 以下列方式连接:
People
有许多Interests
到Peoples_Interests
Interest
有许多Peoples
到Peoples_Interests
我需要向与他们最相似的人民提出建议,这是基于相似兴趣的数量。所以对于示例:
我对棒球,足球和凌空感兴趣。我应该与另一个拥有尽可能多的相似兴趣的用户建议。出现3/3的人应该是我需要的(如果不存在 - 2/3等)。
所以我需要一个查询,输出将按照兴趣相似性Peoples排序。
更新 Db结构:
兴趣
ID
name - string
人民
ID
电子邮件
Peoples_Interests
interests_id
peoples_id
谢谢。
答案 0 :(得分:3)
像这样。
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)
英语(可能会或可能不会更清楚。)
在没有子查询的情况下更新但不太清楚
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)