[英]get Mutual Friends on following and followers relation table
How can I get a mutual friend SQL query? 如何获得共同朋友的SQL查询?
my Sql 我的SQL
But i can not get Mutual Friends 但是我无法得到共同的朋友
id | user_id
--------------
1 | 2
2 | 3
3 | 4
4 | 1
Relation Table
id | follower_id | following_id
--------------------------------
1 | 1 | 2
2 | 2 | 1
3 | 1 | 4
4 | 4 | 3
5 | 4 | 1
Following Query 跟随查询
"SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `following_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` = {$user_id} AND `following_id` <> {$user_id} AND `active` = '1') AND `active` = '1' ";
Follower Query 追随者查询
" SELECT `user_id` FROM " . T_USERS . " WHERE `user_id` IN (SELECT `follower_id` FROM " . T_FOLLOWERS . " WHERE `follower_id` <> {$user_id} AND `following_id` = {$user_id} AND `active` = '1') AND `active` = '1'";
SELECT `user_id` FROM " . T_USERS . "
WHERE `user_id` IN (SELECT CASE WHEN `following_id`={$user_id} THEN `follower_id` ELSE `following_id` END user
FROM " . T_FOLLOWERS . "
WHERE `active` = '1'
AND ((`follower_id` = {$user_id} AND `following_id` <> {$user_id})
OR (`follower_id` <> {$user_id} AND `following_id` = {$user_id}))
GROUP BY user
HAVING count(*)=2
)
AND `active` = '1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.