简体   繁体   English

共同好友sql with join (Mysql)

[英]Mutual friends sql with join (Mysql)

I have two tables我有两张桌子

users table:用户表:

id|name

user_relationships用户关系

id | user_id | friend_id

and want to get names of mutual friends of 2 users.并想获得 2 个用户的共同朋友的名字。 ie: IE:

user_relationships
1 | 1 | 3
2 | 2 | 3  

users
3| sammy

users 1 and 2 have mutual friend 3. I want to get his name 'sammy' in one query.用户 1 和 2 有共同的朋友 3。我想在一次查询中得到他的名字“sammy”。

How do I do that?我怎么做?

You need to join user_relationships with itself, so that two rows with different user_id have the same friend_id您需要将 user_relationships 与自己加入,以便具有不同user_id的两行具有相同的friend_id

All mutual friends:所有共同的朋友:

select ur1.user_id user1, 
       ur2.user_id user2, 
       ur2.friend_id mutual_friend
from   user_relationships ur1 
       JOIN user_relationships ur2 ON  ur1.friend_id = ur2.friend_id
where  ur1.user_id != ur2.user_id

Join with users table to get the names:加入用户表以获取名称:

select ur1.user_id user_id1, 
        u1.name User1, 
       ur2.user_id user2, 
        u2.name User2,
       ur2.friend_id mutual_friend_id,
        u3.name mutual_friend
from user_relationships ur1 
     JOIN user_relationships ur2 ON  ur1.friend_id = ur2.friend_id
     JOIN user u1 ON u1.user_id = ur1.user_id
     JOIN user u2 ON u1.user_id = ur2.user_id
     JOIN user u3 ON ur1.user_id = u3.user_id
where ur1.user_id != ur2.user_id

You can filter for mutual friends for some specific users using ur1.user_id = first_user and ur2.user_id = second_user您可以使用ur1.user_id = first_userur2.user_id = second_user过滤某些特定用户的共同朋友

SELECT id, name
FROM users
WHERE id IN (
  SELECT friend_id
  FROM user_relationships
  WHERE user_id IN ( 1, 2 )
  GROUP BY friend_id
  HAVING COUNT(friend_id) >= 2
)

or with one join:或一次加入:

SELECT friend_id, name
FROM user_relationships r
  INNER JOIN users u ON r.friend_id = u.id
WHERE user_id IN ( 1, 2 )
GROUP BY friend_id
HAVING COUNT(friend_id) >= 2

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.这应该返回 ID 为 @user1_id 和 @user2_id 的用户的所有共同朋友。 It's not tested yet, but should provide a starting point...它尚未经过测试,但应该提供一个起点......

didn't check it but this query should give you a list of related user names for ID [no].没有检查它,但这个查询应该给你一个 ID [no] 的相关用户名列表。

select u1.name, u2.name 
  from users as u1
  join user_relationships as ur
    on u1.id = ur.user_id
  join users as u2
    on ur.friend_id = u2.id
  where U1.id = [no];

BTW, you don't need an artificial id for your cross-table, as (user_id, friend_id) is already a legitimate primary key.顺便说一句,您的交叉表不需要人工 id,因为 (user_id,friend_id) 已经是合法的主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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