对我来说很复杂,因为我是SQL的新手.
我有三个表- Peoples,Interests以及Peoples_Interests(许多一对多) -这是连接方式如下:
People有许多Interests通过 Peoples_Interests
Interest有很多Peoples通过 Peoples_Interests
我需要向人民提出与他们最相似的建议,这些建议基于类似兴趣的数量.因此,对于例如:
我对棒球,足球和凌空感兴趣.我应该与另一个拥有尽可能多的相似兴趣的用户建议.出现3/3的人应该是我需要的(如果不存在 - 2/3等).
所以我需要一个查询输出将按兴趣相似性Peoples排序.
更新: Db结构:
兴趣
ID
名称 - 字符串
人民
ID
电子邮件
Peoples_Interests
interest_id
peoples_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
where interests.id in (select id from interests where interests.peopleid = @inputuserid)
group by people.id, people.name
order by count(interest.id)
Run Code Online (Sandbox Code Playgroud)
用英语(这可能会或可能不会使其更清晰。)
更新后没有子查询,但不太清楚
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)
Run Code Online (Sandbox Code Playgroud)