2017-02-08 82 views
1

我試圖得到的建議朋友說不是我的朋友中,但在我的朋友,朋友的列表。列表建議朋友

2個狀態是一個公認的朋友。

我的會話ID是34,我與用戶3和用戶3的朋友與用戶16的朋友,所以用戶16將顯示作爲建議的朋友,因爲用戶16不是我的朋友。

朋友表

id | user 1_id | user2_id | status 
---------------------------------- 
4  3   34  2 
3  34   3   2 
2  3   16  2 
1  16   3   2 

查詢(我試過)

$user1_id=$_SESSION['id']; 
$user2_id=$data['id']; 
$collectmutualfriendsa = mysqli_query($mysqli," 
SELECT DISTINCT r2.user1_id 
FROM 
friends r 
INNER JOIN friends r2 
ON r.user1_id = r2.user2_id 
LEFT OUTER JOIN friends r3 ON r3.user2_id = r2.user1_id AND r3.user1_id=2 
WHERE r.user2_id = 2 AND r3.user1_id is null"); 

回答

0

好吧,我嘗試了UNION,現在有工作。

$collectmutualfriendsa = mysqli_query($mysqli," 
SELECT * 
FROM friends 
WHERE user2_id NOT IN 
(
    SELECT user1_id FROM friends WHERE user2_id = '$user1_id' 
    UNION 
    SELECT user2_id FROM friends WHERE user2_id = '$user1_id' 
) 

"); 
0
$user1_id = $_SESSION['id']; // 34 
$user2_id = $data['id']; // 3 
$query = "SELECT * FROM Friends_table WHERE user1_id = '$user2_id' AND user2_id != $user1_id;"; 
+0

嗨@vishnu。這個查詢似乎沒有給我正確的結果。它打印出我朋友列表中的用戶3。我想只能得到我的好友列表的朋友打印出來。所以用戶的任何朋友3. – Gateway

+0

檢查可能的,如果你不打印user1_id代替user2_id的 –