我正在尝试根据优先级表'userinterest'中的相同'interestid'为'users'表中的每个用户获取三个匹配用户,如果用户没有兴趣,那么如果他的'gender'与其他用户匹配则获取其中三个用户的用户ID。
用户表具有用户ID和性别,而userinterest表有两列u_id和interest_id WHERE u_id是userid。
每个用户可能有多个兴趣,但即使一个兴趣匹配,我们也可以接受该用户。
我正在寻找的结果类型是两个第一列,其中userid是我们得到的结果,而其他列是group with concatenated三个用户id。
12 | 32,34,23
13 | 33,35,23
14 | 22,35,37
答案 0 :(得分:1)
您可以使用聚合查询来获取两个用户共同拥有的兴趣数量:
select ui.user, ui2.user as otherUsaer, count(*) as numInterests
from userinterest ui join
userinterest ui2
on ui.interest = ui2.interest and ui.user <> ui2.user
group by ui.user, ui2.user;
然后,您可以重新聚合它以获得每个用户的三个最常见用户:
select user, substring_index(group_concat(otherUser order by numInterests desc), ',', 3) as top3
from (select ui.user, ui2.user as otherUsaer, count(*) as numInterests
from userinterest ui join
userinterest ui2
on ui.interest = ui2.interest and ui.user <> ui2.user
group by ui.user, ui2.user
) uu
group by user;
这假设您没有太多用户;否则,用于substring_concat()
的中间字符串缓冲区将溢出。 (可以扩展默认缓冲区长度。)使用变量的替代方法更难编码。
我还建议您不要考虑性别问题。如果真的有兴趣,那么将其添加到兴趣表中。但是,这是一种可以包含性别的方法:
select user, substring_index(group_concat(otherUser order by numInterests desc), ',', 3) as top3
from (select ui.user, ui2.user as otherUsaer,
(count(*) + (u.gender = u2.gender)) as numInterests
from userinterest ui join
userinterest ui2
on ui.interest = ui2.interest and ui.user <> ui2.user join
user u
on ui.user = u.user join
user u2
on ui2.user = u2.user
group by ui.user, ui2.user
) uu
group by user;