简体   繁体   中英

only friends that are not common to both users

Here are my tables:

1): table friends:

 id_invitation     
 id_exp            
 id_des       
 date_invitation
 active            

2): table users:

 id        
 prenom      
 nom          
 email        
 password    

I have two queries:

1: The first returns me list of friends.

SELECT *
  FROM users U
  JOIN friends F
    ON ( U.id = F.id_exp AND F.id_des = :id )
    OR ( U.id = F.id_des AND F.id_exp = :id )
 WHERE U.id <> :id
   AND F.active = 1

2: the second returns only the mutual friends between two users.

SELECT u.id
     , u.nom
     , u.prenom
  FROM users u
  JOIN 
     ( SELECT id_exp
            , id_des
         FROM friends
        WHERE id_exp IN(:id_exp, :id_des)
          AND active = 1
        UNION
       SELECT id_des
            , id_exp
         FROM friends
        WHERE id_des IN(:id_exp, :id_des)
          AND active = 1
     ) tmp  
    ON tmp.id_des = u.id 
 GROUP 
    BY u.id 
HAVING COUNT(*) = 2

Small correction:

1 is a common friend between 2 and 3 since he is a friend with both 2 and 3 (if you want to collect the common friend at 2 and 3, you will have 1). 4 is friend with 2 but not with 3. And 3 also is friend with 5 and 6, I want to search in friends list of 2, recover friends from 2 but who are not friends with 3 (in this case 4). I want to return 4 only and not 4,5,6

More informations: I want that when 3 visits the list of uncommon friends of 2, it sees 4. I want to return only friends of 2 who are not as friendly with 4.

I have a little trouble explaining it.

And I have tried that code:

     SELECT
       *
    FROM
        users U
            INNER JOIN friends F
                ON ( U.id = F.id_exp AND F.id_des = :id )
                OR ( U.id = F.id_des AND F.id_exp = :id )
    WHERE
        (U.id <> :id_k
        AND F.active = 1) 

        AND NOT IN
(SELECT u.id,
      u.nom, u.prenom, u.avatar
    FROM users u
    INNER JOIN
    (
      SELECT id_exp, id_des
      FROM friends
      WHERE id_exp IN(:id_exp, :id_des)
        AND active = 1
      UNION
      SELECT id_des, id_exp
      FROM friends
      WHERE id_des IN(:id_exp, :id_des)
        AND active = 1
    ) tmp ON tmp.id_des = u.id
    GROUP BY u.id
    HAVING COUNT(*) = 2)

But it does not work at worst, it produces errors and the section of the page in question does not appear. Thank you.

I believe you already have the ingredients you need for both mutual and non-mutual friend states using the current query, it's just a matter of how you now use it.

  SELECT
       u.id
     , u.nom
     , u.prenom
     , case when count(tmp.id_des) = 1 then 'Not Mutual'
            when count(tmp.id_des) = 2 then 'Mutual'
            when count(tmp.id_des) = 0 then 'Alone in this world'
            else 'Undefined'
       end as friend_status
  FROM users u
  JOIN 
     ( SELECT id_exp
            , id_des
         FROM friends
        WHERE id_exp IN(:id_exp, :id_des)
          AND active = 1
        UNION
       SELECT id_des
            , id_exp
         FROM friends
        WHERE id_des IN(:id_exp, :id_des)
          AND active = 1
     ) tmp ON tmp.id_des = u.id 
    GROUP BY
       u.id
     , u.nom
     , u.prenom

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM