MYSQL select mutual friends

前端 未结 1 1974
粉色の甜心
粉色の甜心 2020-12-18 15:00

My \'friends\' table has the following columns: id, userID, userID2, state

userID and userID2 don\'t have a specific order of being put into the database.

I

相关标签:
1条回答
  • 2020-12-18 15:36

    After the explanations (and actually reading the "mutual" part):

    SELECT a.friendID
    FROM
          ( SELECT CASE WHEN userID = $id
                          THEN userID2 
                          ELSE userID 
                   END AS friendID 
            FROM friends 
            WHERE userID = $id OR userID2 = $id
          ) a
      JOIN
          ( SELECT CASE WHEN userID = $session
                          THEN userID2 
                          ELSE userID 
                   END AS friendID 
            FROM friends 
            WHERE userID = $session OR userID2 = $session
          ) b
        ON b.friendID = a.friendID 
    
    0 讨论(0)
提交回复
热议问题