简体   繁体   English

如何选择共同的朋友

[英]How to select mutual friends

please i want to select mutual friends of a user and the $my_id is the $_SESSION['user']; 请我要选择一个用户的共同朋友,并且$my_id$_SESSION['user']; while the $user_id is the selected user. $user_id是选定的用户。 here is the code: 这是代码:

    <?php

include 'connect.php';

include 'functions.php';

$my_frnds = mysqli_query{$con, "SELECT * FROM friends WHERE one='$my_id' OR two='$my_id'"};
while($my_friends = mysqli_fetch_array($my_frnds)){
    $one = $my_friends['one'];
    $two = $my_friends['two'];

    if($one == $my_id){
        $user == $two;
    } else
        $user == $one;
}

$user_frnds = mysqli_query{$con, "SELECT * FROM friends WHERE one='$user_id' OR two='$user_id'"};
while($user_friends = mysqli_fetch_array($user_frnds)){
    $user_one = $user_friends['one'];
    $user_two = $user_friends['two'];

    if($user_one == $my_id){
        $users == $user_two;
    } else
        $users == $user_one;
}

?>

lets assume that my friends are (1, 2, 3, 4, 5, 6, 7) and the user friends are (2, 4, 6, 7, 8, 9, 10) so the mutual friends are (2, 4, 6, 7) my question is how do i get the mutual friend out and echo thier ids?. 假设我的朋友是(1、2、3、4、5、6、7),用户朋友是(2、4、6、7、8、9、10),因此共同的朋友是(2、4 ,6,7)我的问题是如何让共同的朋友出去并回荡他们的ID? Sorry i made a mistake, here is the original coding 抱歉我弄错了,这是原始编码

in case one is your id and two is friend id: 如果一个是您的ID,两个是朋友ID:

select * from friends 
inner join (select * from friends where one = $user_id) as temp on temp.two = friends.two
where friends.one = $my_id 
    SELECT * FROM friends AS f1
    INNER JOIN 
    (SELECT * FROM friends WHERE one = $user_id OR two = $user_id) AS f2 
    ON (f1.one = f2.one OR f1.one = f2.two) 
    WHERE f1.one = $my_id OR f1.two = $my_id

hope this helps. 希望这可以帮助。

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

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