【问题标题】:Suggest users based on common interest - MySql query根据共同兴趣推荐用户 - MySql 查询
【发布时间】:2016-06-15 10:17:51
【问题描述】:

基本上我想做的是根据共同兴趣向人们推荐。

我有一个用户表(id、用户名、名字、姓氏等)

我有一个 Interested_People 表,其中存储了 UserID + Interested_in。

我有一个联系人列表表,其中存储了彼此添加的人。(user1,user2,接受 [1,0])

我想要的是选择 * users 表,他们不是我的朋友,他们也和我有同样的兴趣。

我在互联网上搜索了很多,但找不到类似的东西。

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 将是 1。

这个查询随机推荐了 10 个不是我朋友但和我有相同兴趣的用户。但是很慢。

提前致谢!

【问题讨论】:

  • 你的 MySQL 版本是多少?您是否尝试过使用 SQL 查询运行解释?

标签: mysql sql phpmyadmin


【解决方案1】:

您的问题建议进行自我加入以吸引具有共同兴趣的用户。然后,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;

【讨论】:

  • 无法识别的关键字。 (在位置 228 的“not”附近)无法识别的关键字。 (在位置 232 附近“存在”)意外标记。 (在位置 239 的“(”附近)
  • @user2765602 。 . . not 附近没有任何看起来像语法错误的东西。你能设置一个 SQL Fiddle。
  • 我放弃了方括号
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 2021-04-25
  • 2019-05-27
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多