【发布时间】:2015-04-04 17:01:15
【问题描述】:
今天晚上我有点脑子放屁,试图弄清楚我的逻辑。我需要计算一个人与一个用户分享了多少朋友。 (共同的朋友)
我有一个带有用户 ID 和朋友的用户 ID 的表格,我的布局示例如下:
第一个结果意味着 user1 是 user 2 的朋友
[ID] - [FriendsID]
1-2
1-3
1-4
1-15
2-1
2-4
3-1
3-4
4-1
4-2
4-3
4-15
5-15
15-1
15-5
15-4
当我的 PHP 页面加载时,它将加载该用户的好友列表,例如 User1。 这将返回 (2,3,4.15) 的“FriendID”
然后我需要计算人们与用户有多少共同朋友:1 比如,
1 is friends with 2,3,4
2 is friends with 1,4
3 is friends with 1,4,15
This would mean that “2” shares ONE mutual friend with 1
This would mean that “3” shares TWO mutual friend with 1
等等
我的输出需要是 [FriendID] [Count]
朋友ID是朋友
计算与 userID 1 有多少共同朋友
(手动写出的示例数据)
【问题讨论】:
-
在您的示例中,您声明 Nr。 3 与 1 分享了两个共同的朋友,但事实并非如此。也许你的意思是
2,4,15在这一行。我刚注意到。
标签: mysql sql mutual-friendship