简体   繁体   English

有什么方法可以在(1,2)(2,1)友谊场景中找到共同的朋友?

[英]What are ways to get mutual friends in a (1, 2) (2, 1) friendship scenario?

I'm developing a website wherein when a person adds another person as a friend, 2 entries are created. 我正在开发一个网站,当一个人将另一个人添加为朋友时,会创建两个条目。 Let's say uid 1 adds uid 2 as a friend, the following rows are created in MySQL. 假设uid 1uid 2添加为朋友,在MySQL中创建以下行。

activity_id   uid1 uid2
     1         1     2
     2         2     1

uid 2 becomes friends with uid 3 : uid 2uid 3成为朋友:

activity_id   uid1 uid2
     1         1     2
     2         2     1
     3         2     3
     4         3     2

What are the ways to get the mutual friends between uid 1 and uid 2 ? uid 1uid 2之间获取共同朋友的方法有哪些? I'm using php and MySQL with no PDO experience (for now). 我正在使用没有PDO经验的PHP和MySQL(目前)。

[EDIT] Okay, so I decided to organize/normalize the tables. [编辑]好的,所以我决定组织/规范化表格。 The relationship only generates 1 row now. 该关系现在只生成1行。 which makes the table: 这使得表:

activity_id   uid1 uid2
     1         1     2
     2         2     3

Assuming you have called your table 'friends'. 假设你把你的桌子称为'朋友'。

This query will find common friends for users 1 and 3. 此查询将找到用户1和3的常见朋友。

SELECT a.uid2
  FROM friends a
    INNER JOIN friends b
       ON a.uid2 = b.uid2
  WHERE a.uid1 = 1
   AND b.uid1 = 3

Don't you think it might be a bit presumptuous to make 2 rows though? 难道你不认为制作2行可能有点冒昧吗? Maybe uid 2 doesn't really like uid 1 ? 也许uid 2真的不喜欢uid 1? (OTY). (OTY)。

Looking at the table format from your other question , this should do what you want; 从你的其他问题看表格式,这应该做你想要的;

SELECT name FROM users u
JOIN friends f1
  ON u.uid = f1.uid OR u.uid = f1.fid
JOIN friends f2
  ON u.uid = f2.uid OR u.uid = f2.fid
WHERE (f1.uid=1 OR f1.fid=1) AND (f2.uid=3 OR f2.fid=3) 
  AND u.uid<>1 AND u.uid<>3;

Demo here . 在这里演示。

To find all common friends of uid 1 and uid 2 : 要找到uid 1uid 2所有常见朋友:

SELECT t1.uid2
FROM   tbl t1
JOIN   tbl t2 USING (uid2)
WHERE  t1.uid1 = 1
AND    t2.uid1 = 2;

There is one special case, though: this query does not show whether 1 & 2 are friends! 但有一个特例:此查询不显示12是否为朋友!

BTW, your design seems a bit redundant. 顺便说一句,你的设计似乎有点多余。 It should work with just one entry per friendship. 它应该只与每个友谊一个条目一起工作。 (This query would have to be adapted for that, though.) (但是,这个查询必须适应它。)

Basically this is another case of relational division . 基本上这是关系分裂的另一个例子。 There are many different ways to skin this cat. 有很多不同的方法来抚育这只猫。 Find quite under this related question - including performance testing and more information. 找到相关问题 - 包括性能测试和更多信息。


Query for updated question 查询更新的问题

Now there is only 1 row per friendship - a user can pop up in either column: 现在每个友谊只有一行 - 用户可以在任一列中弹出:

SELECT uid
FROM (
    SELECT uid2 AS uid FROM tbl WHERE uid1 = 1
    UNION ALL
    SELECT uid1        FROM tbl WHERE uid2 = 1
    ) u1
JOIN (
    SELECT uid2 AS uid FROM tbl WHERE uid1 = 2
    UNION ALL
    SELECT uid1        FROM tbl WHERE uid2 = 2
    ) u2 USING (uid)
$query="SELECT distinct(uid2) FROM `test` where uid1=$uid union select distinct(uid1) from test where uid2=$uid";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM