PHP / MySQL中的朋友的朋友? [英] Friend of a friend in PHP/MySQL?

查看:84
本文介绍了PHP / MySQL中的朋友的朋友?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于myspace / facebook的社交网络。在我的代码中,你是一个人的朋友,也不是一个朋友,所以我会显示你是朋友的所有动作(在这篇文章中,我将把这些动作简单地称为公告栏,以便更容易可视化。



所以你每次发布公告时都会显示给任何有朋友的人。



在mysql中,你会通过这样做获得一个人的朋友列表

  SELECT user_id FROM friends WHERE friend_id = 1(用户ID)

我想知道一个网站如Facebook和其他一些网站会显示你朋友的所有公告信息,如果有人有想法,请显示一些代码,如什么样的mysql查询?


div class =h2_lin>解决方案

答案是他们没有在朋友表上做选择,他们很可能使用了一个正规化的新闻事件表,饲料类似ar到DoInk.com上的Facebook,这是我们如何做到的:



有一个NewsEvent的概念它有一个类型,一个发起者(一个用户id)和目标用户(也是用户ID)。 (您还可以为与活动相关的其他属性添加其他列,或加入它们)



当用户在其他用户墙上发布内容时,我们会生成事件如下:

  INSERT INTO事件VALUES(wall_post_event,user1,user1)

查看user1的个人资料时,您可以选择所有活动,其中user1是发起者或目标。这就是您显示配置文件Feed的方式。 (您可以根据自己的隐私模式过滤掉事件,您可以考虑在性能方面做这些记录)



示例:

  SELECT * FROM events WHERE initiator = user1或target = user1 //查看其个人资料Feed 

SELECT * FROM events WHERE initiator IN(你的一组朋友ID)//看到你的新闻源

当你想看新闻源对于与您的朋友相关的所有事件,您可以对发起者在您的朋友群中的所有事件进行查询。



避免使用子选择的实现,具体取决于复杂性,他们不会缩放。


I have a social network similar to myspace/facebook. In my code you are either a person's friend or not a friend, so I show all actions from people you are friends with (in this post I will refer to actions as bulletin posts alone to make it easier to visualize.

So you every time a person post a bulletin it will show up to any person who is there friend.

In mysql you would get a persons friend list by doing something like this,

SELECT user_id FROM friends WHERE friend_id = 1 (user ID)

I want to know how a site like facebook and some others would show all bulletin post from your friends and from your friends' friends?

If anyone has an idea please show some code like what kind of mysql query?

解决方案

The answer is that they aren't doing selects on a friend table, they are most likely using a de-normalized news-event table. We implemented a news-feed similar to Facebooks on DoInk.com, here's how we did it:

There is the notion of a "NewsEvent" it has a type, an initiator (a user id) and a target user (also a user id). (You can also have additional column(s) for other properties relevant to the event, or join them in)

When a user posts something on another users wall we generate an event like this:

INSERT INTO events VALUES (wall_post_event, user1, user1)

When viewing user1's profile, you'd select for all events where user1 is either the initiator or the target. That is how you display the profile feed. (You can get fancy and filter out events depending on your privacy model. You may consider doing this in memory for performance reasons)

Example:

SELECT * FROM events WHERE initiator = user1 or target = user1 //to see their profile feed

SELECT * FROM events WHERE initiator IN (your set of friend ids) //to see your newsfeed

When you want to see the newsfeed for all events relative to your friends you might do a query selecting for all events where the initiator is in your set of friends.

Avoid implementations with sub-selects, depending on the complexity, they will not scale.

这篇关于PHP / MySQL中的朋友的朋友?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆