How can I get a mutual friend SQL query?
my 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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.