基本上我要做的是根据共同的兴趣来建议人。
我有一个用户表(id,username,firstname,lastname等)
我有一张Interested_People表,其中存储了UserID + Interested_in。
我有一个联系人列表,其中存储了彼此相加的人。(user1,user2,accepted [1,0])
我想要的是选择不是我朋友的*用户表,他们对我也有同样的兴趣。
我在互联网上搜索了很多但却找不到这样的东西。
Here i do have created a query and it does exactly what I want. But it is very slow. Even it takes 16 to 20 second to output in PHPMyAdmin in my local machine. Now I Kindly request you guys if you can edit my query a bit and make it bandwidth & time efficient.
SELECT *
FROM users
WHERE id IN(SELECT userid
FROM interested_people
WHERE interested_in IN(SELECT interested_in
FROM interested_people
WHERE userid = [userid])
AND id NOT IN(SELECT user1 AS my_friends_userid
FROM contactlist f
WHERE f.user2 = [userid]
AND accepted = 1
UNION
SELECT user2 AS my_friends_userid
FROM contactlist f
WHERE f.user1 = [userid]
AND accepted = 1))
AND id != [userid]
ORDER BY Rand ()
LIMIT 0, 10;
此查询中的[Userid]是在线用户的ID。就像在线我的ID一样。
此查询建议10个随机用户不是我的朋友并且与我有相同的兴趣。但非常懒散。
提前致谢!
答案 0 :(得分:0)
您的问题表明自我加入可以吸引具有共同兴趣的用户。然后,not exists
以避免联系人列表。以下列出了具有共同兴趣的用户列表,按共同兴趣的数量排序:
select ip2.userid, count(*) as numInCommon
from interested_People ipme join
interested_People ip2
on ipme.interested_in = ip2.interested_in and
ipme.userid = $UserId and -- Your user id goes here
ip2.userid <> ipme.userid
where not exists (select 1
from contactlist cl
where cl.user1 = ipme.userid and cl.user2 = ip2.userid and
cl.accepted = 1
) and
not exists (select 1
from contactlist cl
where cl.user1 = ip2.userid and cl.user2 = ipme.userid and
cl.accepted = 1
)
group by ip2.userid;