如何为“你可能知道的人”构建查询

时间:2013-12-21 06:49:10

标签: php mysql

我需要根据用户的当前朋友显示“你可能知道的人”,并且朋友建议应该考虑被阻止的用户,如果用户A阻止用户B,A不应该在他们的朋友中看到用户B或反之亦然 这是我的表结构

mnc_users

id  |   username    |   email       |   fullname
---------------------------------------------------
1   |   userA       |   usera@in.in |   User Alpha
2   |   userB       |   userb@in.in |   User Bravo
3   |   userC       |   userc@im.in |   user c
4   |   userd       |   userd@am.in |   user del

mnc_friends

uid |   fid |   time
-------------------------
1   |   3   |   1363859398  
3   |   1   |   1363859398  
4   |   1   |   1368854898  
1   |   4   |   1368854898  
3   |   2   |   1363846116  
2   |   3   |   1363846116  
2   |   4   |   1363846116  
4   |   3   |   1363846116  

mnc_blocked

id  |   uid |   bid
--------------------
1   |   1   |   2

mnc_frnd_rqsts

uid |   fid
-----------
5   |    1

如何在这种情况下使用php和mysql显示用户建议?

2 个答案:

答案 0 :(得分:2)

试试这个

假设我们正试图让人们接受userA:id = 1

$user_id = 1;

和sql将是

$sql = "    SELECT * FROM mnc_users WHERE id IN(
                                         SELECT fid FROM mnc_friends WHERE uid='$user_id' AND fid!='$user_id'
                                        )
                                  AND id NOT IN(
                                        SELECT bid FROM mnc_blocked WHERE uid='$user_id'
                                       )
   ";

答案 1 :(得分:2)

如果您编辑this answer以添加几项内容,则可以使其正常工作

$user_id = 1;

    $sql = "    SELECT * FROM mnc_users WHERE id NOT IN(
                                             SELECT fid FROM mnc_friends WHERE uid='$user_id' AND fid!='$user_id'
                                            )
                                      AND id NOT IN(
                                            SELECT bid FROM mnc_blocked WHERE uid='$user_id'
                                           )
AND id NOT IN(
                           SELECT fid FROM mnc_frnd_rqsts WHERE uid='$user_id'
                                           )
       ";