简体   繁体   English

SQL 查询共同好友

[英]SQL query for mutual friends

My MySQL tables structure is like this.我的 MySQL 表结构是这样的。

USER
uid

FRIENDS
fuid,fuid2,fapproved

For each friend relationship I insert 2 records in FRIENDS.对于每个朋友关系,我在 FRIENDS 中插入 2 条记录。 If user 1 is friend of user 2 then the next rows are inserted into FRIENDS如果用户 1 是用户 2 的朋友,则将下一行插入 FRIENDS

1,2,1

2,1,1

1,3,1

3,1,1

2,3,1

3,2,1

User id 3 is friend of user id 1 and user id 2用户 id 3 是用户 id 1 和用户 id 2 的朋友

How to get user id 3 in one sql query?如何在一个 sql 查询中获取用户 ID 3?

Given two users @friend1 and @friend2 find all the users who are mutual friends of them:给定两个用户@friend1 和@friend2,找出他们共同好友的所有用户:

SELECT user.uid
FROM user
WHERE EXISTS(
    SELECT TOP 1 1 
    FROM Friends 
    WHERE Friends.fuid = @friend1 AND Friends.fapproved = 1 
      AND Friends.fuid2 = User.uid
  )
  AND EXISTS(
    SELECT TOP 1 1 
    FROM Friends 
    WHERE Friends.fuid = @friend2 AND Friends.fapproved = 1 
      AND Friends.fuid2 = User.uid
  )
$query = "SELECT DISTINCT a.* FROM user  a, visitor b, visitor c WHERE b.user_id = c.visitor_id  AND b.visitor_id = a.user_id AND c.user_id = a.user_id AND b.user_id=65";

i tried it.我尝试过这个。 it is working.这是工作。

Try this:尝试这个:

SELECT DISTINCT a.*
    FROM user  a, friends b, friends c
WHERE b.fuid = c.fuid2
    AND b.fuid2 = a.uid
    AND c.fuid = a.uid
    AND b.fapproved = 1
    AND c.fapproved = 1

Test Script(Tried it in MS SQL..):测试脚本(在 MS SQL 中试过):

CREATE TABLE #USER
(
    uid INT
)

INSERT #USER VALUES(1)
INSERT #USER VALUES(2)
INSERT #USER VALUES(3)


CREATE TABLE #FRIENDS
(
    fuid INT,
    fuid2 INT,
    fapproved  INT
)


INSERT #FRIENDS VALUES(1,2,1)
INSERT #FRIENDS VALUES(2,1,1)
INSERT #FRIENDS VALUES(1,3,1)
INSERT #FRIENDS VALUES(3,1,1)
INSERT #FRIENDS VALUES(2,3,1)
INSERT #FRIENDS VALUES(3,2,1)


SELECT DISTINCT a.*     
    FROM #user  a, #friends b, #friends c 
 WHERE b.fuid = c.fuid2     
     AND b.fuid2 = a.uid     
   AND c.fuid = a.uid     
   AND b.fapproved = 1     
     AND c.fapproved = 1 

IMO it's more intuitive to find mutual friends with a self-join on the friendships table IMO 在友谊表上找到自我加入的共同朋友更直观

SELECT a.fuid2 FROM facebook_friendships AS b
    ON a.fuid = :user_a AND b.fuid = :user_b AND a.fuid2 = b.fuid2
    AND a.fapproved = 1 AND b.fapproved = 1

Here are the important conditions in English:以下是英语的重要条件:

  • a.fuid =:user_a - user A is friends with user X in friendship A a.fuid =:user_a - 用户 A 是用户 X 的好友 A
  • b.fuid =:user_b - user B is friends with the user Y in friendship B b.fuid =:user_b - 用户 B 与用户 Y 是好友 B 的好友
  • a.fuid2 = b.fuid2 - user A and user B are friends with the same user (ie, X and Y are the same user) a.fuid2 = b.fuid2 - 用户A和用户B是同一个用户的朋友(即X和Y是同一个用户)

So you are basically grabbing all the rows that the conditions I listed above.所以你基本上抓住了我上面列出的条件的所有行。 You may get duplicate results so adding DISTINCT around a.fuid2 would be a good idea.您可能会得到重复的结果,因此在 a.fuid2 周围添加 DISTINCT 是个好主意。

i'm just starting MySQL and i faced this problem.我刚开始 MySQL 我遇到了这个问题。

personID    friendID
6           10
6           2
6           3
8           1
8           2
8           3

/*  query for friends   */
select f.personID, p.personID, firstName, lastName
from person p
inner join friends f on f.friendID = p.personID
where f.personID = 6;

/*  query for common friends    */
select f1.personID 'personID 1', f2.personID 'personID 2', f1.friendID 'common friend'
from person p
inner join friends f1 on f1.friendID = p.personID
inner join friends f2 on f2.friendID = p.personID
where f1.personID = 6 and f2.personID = 8 and f1.friendID = f2.friendID;

result:结果:

personID 1    personID 2    common friend
6             8             2
6             8             3

although it's not a direct solution to your problem, you may base the solution here.虽然它不是您问题的直接解决方案,但您可以在此处建立解决方案。

$query = "SELECT f.fuid, f.fuid2 FROM FRIENDS f, USER u WHERE (f.fuid = u.uid or f.fuid2 = u.uid) AND (u.uid = ".$uid.";");

is that what are you searching for?那是你在寻找什么?

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

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