简体   繁体   English

共同的朋友PHP

[英]Mutual Friends PHP

So usually I wouldn't ask from help like this, so this is a first. 所以通常我不会这样求助,所以这是第一次。 But I've been racking my brains for two days now over a query that is most likely simple. 但是,对于一个很可能很简单的查询,我已经花了两天的时间。

I'm wanting to get the count and the actual users printed out that our mutual to each other just like facebook. 我想获取数量,并且实际用户打印出我们彼此一样,就像facebook一样。

My tables are as follows. 我的表格如下。

Friends id | 朋友编号| user1_id | user1_id | user2_id | user2_id | status 状态

User id | 用户名| first | 第一| last 持续

So I grasp the simple idea behind it. 因此,我掌握了背后的简单想法。 if I'm friends with someone, and they're also friends with that person that person becomes a mutual friend. 如果我是某人的朋友,而他们也是该人的朋友,则该人成为共同的朋友。

I've tried the below with no real results and tried so many other examples offline and to put it frank I don't understand inner-joins as I use them rarely so if someone could come up with something and possibly explain each part that may help me and others work out something for the future. 我尝试了下面的方法,但没有实际结果,并且离线尝试了许多其他示例,并且坦率地说,我不了解内部联接,因为我很少使用它们,所以如果有人可以提出一些建议并可能解释每个可能帮助我和其他人为将来工作。

My session id is under $user1_id and user 2 is under $user2_id which are all sanitized against MYSQLI injection if this helps! 我的会话ID在$ user1_id下,用户2在$ user2_id下,如果有帮助,都针对MYSQLI注入进行了清理!

Thank you to anyone that can help me. 谢谢任何能帮助我的人。

 $mutual_friends = mysqli_query($mysqli,"
SELECT user.id
FROM user
WHERE EXISTS(
    SELECT friends.user1_id, friends.user2_id, friends.status
    FROM friends 
    WHERE friends.user1_id = '$user1_id' AND friends.status = 2 
      AND friends.user2_id = user.id
  )
  AND EXISTS(
    SELECT friends.user1_id, friends.user2_id, friends.status
    FROM friends 
    WHERE friends.user1_id = '$user2_id' AND friends.status = 2 
      AND friends.user2_id = user.id
  )
");
$mutualfriend=mysqli_fetch_array($mutual_friends);
$mutualfriendcount=mysqli_num_rows($mutual_friends);
The mutual friend count by number
echo"".$mutualfriendcount.";

var_dump($mutual_friends );
while($mutualfriend=mysqli_fetch_array($mutual_friends)){

The list of users by name and id 

}
$mutual_friends = mysqli_query($mysqli,"
SELECT     user.user_id,
           user.first,
           user.last
FROM       user
INNER JOIN friends as friends1
       ON  friends1.user2_id = user.id
       AND friends1.user1_id = " . $user1_id . "
       AND friends1.status = 2
INNER JOIN friends as friends2
       ON  friends2.user2_id = user_id
       AND friends2.user1_id = " . $user2_id . "
       AND friends2.status = 2
");

Explanation: 说明:

First of all, your user_id columns are numerical, so you should not wrap their values in quotes. 首先,您的user_id列是数字列,因此您不应将其值括在引号中。

SELECT ... FROM user has no secrets, it select all users. SELECT ... FROM user没有秘密,它将选择所有用户。

Then the INNER JOIN with friends will find those records in friends that have the user1_id set to your connected user and express a friendship with the user record it is joined to. 然后INNER JOINfriends会发现,在这些记录friends是有user1_id设置为您连接的用户,并表示与它加入了用户记录了友谊。 Now if there is no such record in friends , then the user record is filtered away from the result set. 现在,如果friends没有此类记录,则将user记录从结果集中过滤掉。 So in essence after the first INNER JOIN we are left with user records that correspond to the friends of the connected user. 因此,本质上,在第一个INNER JOIN之后,我们留下的用户记录对应于所连接用户的朋友。

Then the second INNER JOIN again tries to find friends, but with one difference. 然后第二个INNER JOIN再次尝试寻找朋友,但有一个区别。 This time user1_id must match with the given $user2_id . 这次, user1_id必须与给定的$user2_id匹配。 So this is limited to friends of user #2. 因此,这仅限于用户#2的朋友。 Now some of these friends were already filtered out from the result set after the first INNER JOIN was applied, so now we only reduce the result set further by taking out the users that are not friends of user #2. 现在,在应用了第一个INNER JOIN之后,这些朋友中的一些已经从结果集中过滤掉了,所以现在,我们仅通过去除不是用户#2朋友的用户来进一步减少结果集。

And that gives the desired end result. 这样就可以达到预期的最终结果。

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

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