社交网络 - 推荐的朋友

时间:2013-11-25 07:51:46

标签: mysql sql

我有两个简单的MySQL表:user&关系。

关系表:

user_id int(10) unsigned    NO  PRI     
friend_id   int(10) unsigned    NO  PRI 

(部分)用户表:

id  int(10) unsigned    NO  PRI     
username    varchar(128)    NO

我使用此查询选择朋友的朋友:

SELECT f2.friend_id, u.username
FROM relations f1
JOIN relations f2 ON f1.friend_id=f2.user_id
LEFT JOIN user u ON u.id = f2.friend_id
WHERE f2.friend_id NOT IN (select friend_id from relations where user_id=@user_id) AND f1.user_id= 2 AND f2.friend_id!= 2

但是我还需要得到建议的朋友......(小组中知道2个或更多直接朋友的人),我有这个问题。有什么好方法(查询,或者我应该用PHP做什么?)来建议朋友?

2 个答案:

答案 0 :(得分:5)

考虑以下内容......此示例假定通过每个友谊插入两行来建立往复。但是,为简单起见,以下示例并未检查友情是否得到回应!

 DROP TABLE IF EXISTS friends;

 CREATE TABLE friends
 (initiator VARCHAR(12) NOT NULL
 ,reciprocator VARCHAR(12) NOT NULL
 ,PRIMARY KEY (initiator,reciprocator)
 );

 INSERT INTO friends VALUES 
 ('Adam','Ed'),
 ('Ed','Adam'),
 ('Adam','Ben'),
 ('Ben','Adam'),
 ('Adam','Charlie'),
 ('Charlie','Adam'),
 ('Adam','Dan'),
 ('Dan','Adam'),
 ('Ed','Ben'),
 ('Ben','Ed'),
 ('Ben','Charlie'),
 ('Charlie','Ben'),
 ('Charlie','Dan'),
 ('Dan','Charlie'),
 ('Dan','Fred'),
      ('Fred','Dan'),
 ('Adam','Fred'),
      ('Fred','Adam');

要获得Ben的所有“朋友的朋友”的列表,我们可以这样做......

 SELECT y.reciprocator
   FROM friends x
   JOIN friends y
     ON y.initiator = x.reciprocator
    AND y.reciprocator <> x.initiator
   LEFT
   JOIN friends z
     ON z.reciprocator = y.reciprocator 
    AND z.initiator = x.initiator
  WHERE x.initiator = 'Ben'
    AND z.initiator IS NULL;
 +--------------+
 | reciprocator |
 +--------------+
 | Dan          |
 | Fred         |
 | Dan          |
 +--------------+

正如你所看到的,因为丹是亚当和查理(本的两个朋友)的朋友,他的名字出现了两次。

因此,要获得DISTINCT朋友的朋友列表,只需包含DISTINCT运算符。

同样地,为了获得Ben的陌生人名单,以及至少Ben的两个朋友的朋友,我们可以这样做......

SELECT y.reciprocator
  FROM friends x
  LEFT
  JOIN friends y
    ON y.initiator = x.reciprocator
   AND y.reciprocator <> x.initiator
  LEFT
  JOIN friends z
    ON z.reciprocator = y.reciprocator
   AND z.initiator = x.initiator
 WHERE x.initiator = 'Ben'
   AND z.initiator IS NULL
 GROUP
    BY y.reciprocator
HAVING COUNT(*) >= 2;
 +--------------+
 | reciprocator |
 +--------------+
 | Dan          |
 +--------------+

处理这个问题的互惠方面可能有几种方法,就像处理互惠本身有几种方法一样。

一种方法是用一个简单的子查询替换上面friends表的每个匹配项,例如:

SELECT y.reciprocator
  FROM (SELECT a.* FROM friends a JOIN friends b ON b.reciprocator = a.initiator AND b.initiator = a.reciprocator) x

  LEFT
  JOIN (SELECT a.* FROM friends a JOIN friends b ON b.reciprocator = a.initiator AND b.initiator = a.reciprocator) y
    ON y.initiator = x.reciprocator
   AND y.reciprocator <> x.initiator

  LEFT
  JOIN (SELECT a.* FROM friends a JOIN friends b ON b.reciprocator = a.initiator AND b.initiator = a.reciprocator) z
    ON z.reciprocator = y.reciprocator
   AND z.initiator = x.initiator

 WHERE x.initiator = 'Ben'
   AND z.initiator IS NULL
 GROUP
    BY y.reciprocator
HAVING COUNT(*) >= 2; 

答案 1 :(得分:1)

谢谢@Strawberry,太糟糕了我无法立即使用你的解决方案......我的设计(oxwall)完全不同:

+--------------+--------------+--------------+--------------+
| userId       | friendId     | status       | whatever     | 
+--------------+--------------+--------------+--------------+
| 1            | 3            | request      | Dan          |
| 3            | 6            | active       | 
| 1            | 7            | ignore
+--------------+

没有重复1-&gt; 2然后2-> 1喜欢

所以我的查询:

    SELECT
        y.friendId,
        COUNT(*) AS totalFriends
    FROM
        `ow_friends_friendship` x
    LEFT JOIN `ow_friends_friendship` y 
                ON y.userId = x.friendId         

    WHERE
        x.userId = xxx
    AND  y.friendId not in (SELECT userId FROM ow_friends_friendship WHERE friendId = xxx)
    AND  y.friendId not in (SELECT friendId  FROM ow_friends_friendship WHERE userId = xxx)

    GROUP BY
        y.friendId
    HAVING
        totalFriends >= 2
    ORDER BY
        totalFriends DESC

希望将来可以帮助某人