Mutual friends sql with join (Mysql)

前端 未结 4 1122
情书的邮戳
情书的邮戳 2020-12-06 03:41

I have two tables

users table:

id|name

user_relationships

id | user_id | friend_id

and want to ge

4条回答
  •  爱一瞬间的悲伤
    2020-12-06 04:34

    You could try something like this:

    select id, name from users where id in 
        (select friend_id from user_relationships where user_id = @user1_id and friend_id in 
            (select friend_id from user_relationships where user_id = @user2_id)
        )
    

    This should return all mutual friends of users with the IDs @user1_id and @user2_id. It's not tested yet, but should provide a starting point...

提交回复
热议问题