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

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

问题描述

我有一个类似于 myspace/facebook 的社交网络.在我的代码中,你要么是某个人的朋友,要么不是朋友,所以我展示了你朋友的所有行为(在这篇文章中,我将行为单独称为公告帖子,以便于可视化.

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.

在 mysql 中,你可以通过做这样的事情来获得一个人的朋友列表,

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)

我想知道像 facebook 和其他一些网站这样的网站如何显示来自您的朋友和您朋友的朋友的所有公告?

如果有人有想法,请展示一些代码,比如什么样的 mysql 查询?

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

推荐答案

答案是他们没有在朋友表上进行选择,他们很可能使用非规范化的新闻事件表.我们在 DoInk.com 上实现了一个类似于 Facebook 的新闻源,我们是这样实现的:

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:

有一个新闻事件"的概念,它有一个类型、一个发起者(一个用户 ID)和一个目标用户(也是一个用户 ID).(您还可以为与事件相关的其他属性添加额外的列,或加入它们)

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)

查看用户 1 的个人资料时,您会选择用户 1 是发起者或目标的所有事件.这就是您显示个人资料提要的方式.(您可以根据您的隐私模型进行花哨和过滤事件.出于性能原因,您可以考虑在内存中执行此操作)

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)

示例:

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天全站免登陆