Facebook或LinkedIn喜欢连接建议配置文件提醒


Facebook or LinkedIn like connection suggestion profile alert

我有一个"users"表,如下所示:

+-----+------------+---------+---------------+
| uid | first_name | surname |         email |
+-----+------------+---------+---------------+
      1        joe    bloggs    joe@test.com
      2       bill    bloggs   bill@test.com
      3       john    bloggs   john@test.com
      4       karl    bloggs   karl@test.com

和"连接"表,如下所示:

+----+---------+----------+--------+
| id | user_id | user_id2 | status |
+----+---------+----------+--------+
   1         1          3        1
   2         3          1        1
   3         4          3        1
   4         3          4        1
   5         2          3        1
   6         3          2        1

这里id是auto auto_increment用户id保存在user_id或user_id2。状态1表示连接已被批准并且处于活动状态。

现在我想发送电子邮件提醒用户与个人资料的建议,如Facebook或LinkedIn做。我认为有可能在用户之间获得相互连接,但不确定如何做。我试过了,但并不完美。我想得到这些都与一个mysql查询与用户和他们建议的连接配置文件。知道怎么做吗?

提前感谢!

这样的算法从来都不是完美的:你永远不可能确切地知道两个人是否认识对方。人们可能住在同一栋楼里,做同样的工作,有100个共同的朋友,甚至有相同的爱好,但彼此却不认识(当然这种可能性不是很大)。

社交网络到底做什么当然是未知的(这是他们赚钱的一部分方式)。但有些方面是已知的。例如,共同朋友的数量很重要(还有例如地点、兴趣、爱好、教育程度、工作、姓氏等)。

根据你提供的信息,一个人可以或多或少地只使用共同好友的数量。这可以使用以下查询完成:

SELECT a.user_id, b.user_id2, count(*) --Select the two ids and count the number of transitive relations
FROM connections as a, connections as b --Use the table twice (transitivity)
WHERE a.user_id2 = b.user_id  -- Transitivity constraint
      AND a.user_id < b.user_id2 -- Maintain strict ordening (can be dropped when checked)
      AND a.status = 1 -- First relation must be confirmed.
      AND b.status = 1 -- Second connection must be confirmed.
      AND NOT EXISTS ( -- Not yet friends
          SELECT *
          FROM connections as c
          WHERE c.user_id = a.user_id
                AND c.user_id2 = b.user_id2
      )
GROUP BY a.user_id, b.user_id2 -- Make sure we count them correctly.

正如你在这里看到的,小提琴计算出(1,2), (1,4)(2,4)还不是朋友,并且都有一个共同的朋友。

一旦共同朋友的数量超过一定的阈值,就可以提出友谊。

然而,我会建议您使您的表更紧凑:向表添加CHECK,使user_id始终严格小于user_id2 (CHECK(user_id < user_id2))。这使得数据库更加紧凑,对于数据库工具的大多数实现也更快,查询也更简单。(1,3,1)(3,1,1)到底有什么区别?