SELECT DISTINCT(b.friend_id) AS possible_id
FROM friend_friends a
JOIN friends_friends b
ON b.user_id = a.friend_id
LEFT JOIN friends_not_friends n
ON b.friend_id = n.friend_id
AND n.user_id = a.user_id
AND n.not_friends = 1
WHERE a.user_id = 1703122278
AND n.friend_id IS NULL
這顯示了
(1st list of friends of friends of 1703122278)
MINUS
(2nd list of not friends of 1703122278)
我希望你不要」 t想要
list of friends of
(friends of 1703122278
minus
(2nd list of not friends of 1703122278)
)
只是在鬼混,這裏是查詢使用NOT IN
。 我個人覺得這些更清晰,但是他們在速度方面可能效率較低。
-- Friends of friends of a user
SELECT DISTINCT(b.friend_id) AS possible_id
FROM friend_friends b -- the b and a aliases
WHERE b.user_id IN -- can be removed
(SELECT a.friend_id
FROM friends_friends a
WHERE a.user_id = 1703122278
)
;
和問了一句:
-- Friends of friends of a user that
-- are also not not_friends of the user:
SELECT DISTINCT(b.friend_id) AS possible_id
FROM friend_friends b -- the b, a and n aliases
WHERE b.user_id IN -- can be removed too
(SELECT a.friend_id
FROM friends_friends a
WHERE a.user_id = 1703122278
)
AND b.friend_id NOT IN
(SELECT n.friend_id
FROM friends_not_friends n
WHERE n.user_id = 1703122278
AND n.not_friends = 1
)
;
使用子查詢有什麼問題? – 2011-03-23 21:33:23