简体   繁体   English

从Facebook好友那里得到共同的朋友(MySQL表)

[英]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 时间戳

  • id = unique id for the record id =记录的唯一ID
  • facebook_id = the friends facebook id facebook_id =朋友的脸书ID
  • name = the friends name name =朋友的名字
  • uid = the users uid on my site, which is a friend to the person saved in the table uid =我网站上的用户uid,它是表格中保存的人的朋友
  • timestamp = don't need to explain :-) 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.

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