SQL Query to find number of mutual friends given a table of person, friend pair

后端 未结 1 1750
孤城傲影
孤城傲影 2021-01-26 01:36

I have searched for other questions and I was able to solve part of what I wanted but I couldn\'t get any further from that.

I have a table with two columns(user, friend

相关标签:
1条回答
  • 2021-01-26 02:23

    You can use a self-join:

    select f1.user as user1, f2.user as user2, count(*) as num_in_common
    from friends f1 join
         friends f2
         on f1.friend = f2.friend 
    group by f1.user, f2.user;
    

    You can add a where clause if you want this information for a particular pair of users.

    0 讨论(0)
提交回复
热议问题