[英]Get mutual friends from Facebook friends (MySQL table)
I'm trying to get mutual friends between 2 people. 我想要两个人之间的共同朋友。 I've saved the persons friends in a table called "friends" with the following fields:
我已将这些人朋友保存在一个名为“朋友”的表格中,其中包含以下字段:
id | id | facebook_id |
facebook_id | name |
名字| uid |
uid | timestamp
时间戳
Hope it make sense, have tried various ways to get the friends, but without luck 希望它有意义,尝试各种方式来结交朋友,但没有运气
I dont know if using subqueries would be faster or mySQL already optimizes it. 我不知道使用子查询会更快还是mySQL已经优化了它。 Solution with subqueries:
子查询解决方案:
SELECT f1.id, f1.name
FROM (SELECT id, name FROM friends WHERE uid=1) f1
JOIN (SELECT id, name FROM friends WHERE uid=2) f2
ON f1.id=f2.id;
It would be nice if MySQL has an INTERSECT
operator, but... 如果MySQL有一个
INTERSECT
运算符会很好,但......
You can grab all the friends of one person: 你可以抓住一个人的所有朋友:
SELECT id, name
FROM friends
WHERE uid = 1
You could then JOIN this list back to the friends table, looking for the same friend for the other user: 然后,您可以将此列表加入到好友表中,为其他用户寻找同一个好友:
SELECT f1.id, f1.name
FROM friends f1
JOIN friends f2 on f1.id = f2.id
WHERE f1.uid = 1
and f2.uid = 2
It's pseudo-code, but it should be close. 这是伪代码,但应该很接近。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.