简体   繁体   English

MYSQL检索几个用户的共同朋友

[英]MYSQL Retrieve mutual friends for several users

I am creating a small social network in which when a user posts anything.I will get ids of his friends to whom he referred the post.Now the problem is that the users who will be referred can be between 1 or 10 . 我正在创建一个小型的社交网络,当用户发布任何内容时,我会得到他推荐该帖子的朋友的ID。现在的问题是,被推荐的用户可能在1 or 10之间。 So now suppose that a user posted a status and referred 6 friends. 因此,现在假设用户发布了一个状态并推荐了6个朋友。

Now i need to find mutual friends between the user who posted the status and the people referred one by one.Meaning that suppose user who posted the status has id 1 and the people who are referred have 2,3,4,5,6,7 . 现在我需要在发布状态的用户和一个被推荐的人之间找到共同的朋友。这意味着假设发布状态的用户的ID为1 ,被推荐的人的ID为2,3,4,5,6,7

So what the query will do it will find mutual friends between 1 and 2 then 1 and 3 then 1 and 4 and so on till 7.So at the end i will be getting a result containing the mutual friends between the user who posted and the people who are referred(or tagged we can say). 因此,查询将执行的操作将在1 and 2之间找到共同的朋友,然后在1 and 3之间找到1 and 4 ,依此类推,直到7。最后,我将得到一个结果,其中包含发布用户和被推荐(或可以标记)的人。

I've two tables User and Friends 我有两个表UserFriends

 User Table:                                Friends Table:
--------------------------                 -----------------------------------
 UserID    Name                            friendsid     friendname     userid
--------------------------                 -----------------------------------
 1097517748         User1                       3          friend1     536772587                     
 100003052455644    User2                       8          friend2     100003052455644
 536772587          User3                       8          friend3     1097517748
 4                  User4                       3          friend4     100003052455644

Now the friends table can contain different people who does or does not exist in the user table.In this example user 2 and 3 has friend with id 8 in common.I've got my guy but that guy id ie 8 must be present in the user table also.This is only between userid 2 and 3. I will be getting several userid and i've find mutual friends of all the friends that are referred with the guy who has posted the status. 现在的朋友表可以包含不同的人谁做,或在用户不存在table.In这个例子用户2和3有朋友在common.I've ID 8得到了我的人,但那个家伙ID IE 8必须出现在用户表也是。这仅在用户ID 2和3之间。我将得到几个用户ID,并且已经找到与发布状态的人引用的所有朋友的共同朋友。

I hope I've cleared it.If I get the idea for the query I can generate it dynamically using php for different number of referred friends. 我希望我已经清除它。如果我对查询有想法,可以使用php为不同数量的推荐朋友动态生成它。

So far I've written this query. 到目前为止,我已经编写了此查询。

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748

But this is only bringing me mutual friends between two user.When I add a third guy lets say f3 and compare it with f1 it does not bring any result and another problem with this query is that it is not verifying that the mutual friend id exists in the user table or not.After adding the 3rd guy. 但是,这只会给我带来两个用户之间的共同朋友。当我添加第三个家伙时,让我们说f3并将其与f1进行比较不会带来任何结果,并且此查询的另一个问题是它没有验证共同朋友的ID是否存在是否在用户表中添加第三个家伙后

SELECT count(f1.friendsid) FROM friends AS f1
INNER JOIN friends AS f2 ON f1.friendsid = f2.friendsid
INNER JOIN friends AS f3 ON f1.friendsid = f3.friendsid
WHERE f1.userid = 100003052455644
AND f2.userid = 1097517748
AND f3.userid = 536772587

This query does not bring any results.So I wanted to create a query that does all this. 该查询没有任何结果。因此,我想创建一个执行所有这些操作的查询。

EDIT: 编辑:

Actually I am using facebook connect and I save all the users friends in my database when users login, the user table will contain only those users who are registered on my website. 实际上,我使用的是facebook connect,当用户登录时,我将所有用户朋友保存在数据库中,用户表将仅包含那些在我的网站上注册的用户。

I finally figured it out. 我终于弄明白了。 The query was 该查询是

SELECT DISTINCT(friendsid) FROM (
SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644 and f2.userid =1097517748 
INNER JOIN user ON  f2.friendsid = user.userid and f1.friendsid = user.userid 
UNION ALL
SELECT  (f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644  and f2.userid =536772587
INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
UNION ALL
SELECT DISTINCT(f1.friendsid) FROM friends AS f1 
INNER JOIN friends AS f2 ON f1.friendsid=f2.friendsid 
and f1.userid = 100003052455644  and f2.userid =694250830
INNER JOIN user ON  f1.friendsid = user.userid and f2.friendsid = user.userid 
) a

This query will find the id's of all the mutual friends between the user who posted the status and the users whom he refered. 此查询将查找发布状态的用户和他引用的用户之间的所有共同朋友的ID。

The user with id 100003052455644 is the person who posted status and all the others are his friends whom he referred.The DISTINCT will make sure that the id's are unique and not repeated.This is for 3 persons he referred.We can add the UNION ALL part dynamically setting the id's and this will help us create a dynamic query for retrieving mutual friends for different number of referred people. 与ID的用户100003052455644是谁贴的地位的人,所有的人都是他的朋友被他referred.The DISTINCT将确保该ID的是唯一的,而不是repeated.This为3口人,他referred.We可以添加UNION ALL部分会动态设置ID,这将有助于我们创建动态查询,以检索不同数量的推荐人的共同朋友。

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

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