簡體   English   中英

在關注者和關注者關系表上獲取共同朋友

[英]get Mutual Friends on following and followers relation table

如何獲得共同朋友的SQL查詢?

我的SQL

但是我無法得到共同的朋友

在此處輸入圖片說明

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

跟隨查詢

"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' ";

追隨者查詢

" 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM