过滤掉加入

时间:2011-03-23 22:08:01

标签: mysql

我有这个问题:

SELECT DISTINCT(b.friend_id) AS possible_id
FROM friends_friends a
    JOIN friends_friends b 
      ON b.user_id = a.friend_id
WHERE a.user_id = 123456789

它的基本功能是通过吸引用户朋友朋友来获取可能的朋友列表

现在我要做的是过滤掉特定用户朋友的列表

以下是获取该用户朋友的示例

SELECT friend_id
FROM friends_friends
WHERE user_id = 123456789

我想通过加入来实现这一点,我知道它很简单,但我似乎无法绕过它。

3 个答案:

答案 0 :(得分:0)

您可以在where子句中添加NOT IN子句:

and b.friend_id NOT IN (SELECT friend_id
FROM friends_friends
WHERE user_id = 123456789)

答案 1 :(得分:0)

SELECT DISTINCT(b.friend_id) AS possible_id
FROM friends_friends a
     JOIN friends_friends b 
       ON b.user_id = a.friend_id
LEFT JOIN friends_friends n
       ON b.user_id = n.friend_id AND n.user_id = 123456789 
WHERE a.user_id = 123456789 AND n.friend_id IS NULL

我想我做对了......

答案 2 :(得分:0)

SELECT DISTINCT(b.friend_id) AS possible_id
FROM friends_friends a
    JOIN friends_friends b 
      ON b.user_id = a.friend_id
    LEFT JOIN friends_friends c
      ON c.user_id = a.user_id
     AND b.family_id = c.friend_id
WHERE a.user_id = 123456789
AND c.friend_id IS NULL

这对我有用:)