如何建立友谊关系模型

发布于 2024-12-08 02:39:20 字数 1083 浏览 2 评论 0原文

我一直在试图弄清楚如何做到这一点,即使查看其他示例,我也无法弄清楚,所以也许我可以获得一些个性化的帮助。

我有两个表,users_statusfriendships

users_status 表中,我有一个字段 userid 和其他几个字段。 在 friendships 表中,我有字段 request_torequest_fromfriendship_status

基本上我想要做的是获取当前用户和当前用户的朋友的所有状态帖子(我可以在 PHP 中使用 $userid 变量指定)。

下面是 friendships 表结构的示例。当发送好友请求时,发送者和接收者的 userid 被放入表中,friendship_status 为 0。当请求被接受时,friendship_status 被设置为 1,这两个人现在是好友。

friendship_id   request_from    request_to  friendship_status
1               111248          111249      1
2               111209          111249      1
3               111209          111248      0
11              111209          111259      1
5               111252          111209      1
12              111261          111209      1

我意识到这甚至可能不是确定友谊的最佳结构,特别是因为该网站是基于关系的,并且必须检查友谊连接将是一个经常使用的事情。

对于 friend_requestsfriendships 有两个单独的表可能会更好吗?如果是这样,我将如何构建/管理friendships表?

I have been trying to figure out how to do this, and even with looking at other examples, I can't get it figured out, so maybe I can get some personalized help.

I've got two tables, users_status and friendships.

In the users_status table I have a field userid, and several others.
In the friendships table, I have the fields request_to,request_from, and friendship_status.

Basically what I want to do is get all of the status posts by the current user AND those who are friends of the current user (which I can specify in my PHP using a $userid variable).

Here's an example of the friendships table structure. When a friend request is sent, the userid of the sender and receiver are placed in the table, with a friendship_status of 0. When the request is accepted, the friendship_status is set to 1 and those two are now friends.

friendship_id   request_from    request_to  friendship_status
1               111248          111249      1
2               111209          111249      1
3               111209          111248      0
11              111209          111259      1
5               111252          111209      1
12              111261          111209      1

I realize this may not even be the best structure for determining friendships, especially since the site is relationship based and having to check for friendship connections will be a frequently used thing.

Would it perhaps be better to have two separate tables for friend_requests and friendships? If so, how would I structure/manage the friendships table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海少女心 2024-12-15 02:39:20

您可以使用表连接(例如 http://dev.mysql.com /doc/refman/5.0/en/join.html)来查找所有请求。

实际上你可以在这里使用子查询:

SELECT * FROM users_status WHERE userid = "$userid" 
    OR userid in (SELECT request_to   FROM friendships where request_from = "$userid" AND friendship_status = 1)
    OR userid in (SELECT request_from FROM friendships where request_to   = "$userid" AND friendship_status = 1)

$userid 替换为你的用户 ID

You can use a table join (e.g. http://dev.mysql.com/doc/refman/5.0/en/join.html) to find all of the requests.

Actually you can use a subquery here:

SELECT * FROM users_status WHERE userid = "$userid" 
    OR userid in (SELECT request_to   FROM friendships where request_from = "$userid" AND friendship_status = 1)
    OR userid in (SELECT request_from FROM friendships where request_to   = "$userid" AND friendship_status = 1)

replace $userid with your user id

满栀 2024-12-15 02:39:20

我能想到的最简单的模式是:

PENDING_FRIENDSHIPS(request_from, request_to)
FRIENDSHIPS(request_from, request_to)

我还删除了 ID,因为两个表上的两个字段都将是复合主键(request_from、request_to)。

要获取当前用户的所有朋友,只需运行:

select * from friendships
where $currentUser = request_from OR $currentUser = request_to

这将返回两列,并且您必须在 PHP 中删除当前用户。

从该模式中获取所有朋友的另一种方法是运行 UNION:

select request_from from friendships
where request_to = $currentUser
UNION
select request_to from friendships
where request_from = $currentUser

该解决方案的缺点是您正在运行 2 个选择

The simplest schema I can think of is:

PENDING_FRIENDSHIPS(request_from, request_to)
FRIENDSHIPS(request_from, request_to)

I also removed the ID because both fields on both tables will be compound primary keys (request_from, request_to).

To get all friends from the current user just run:

select * from friendships
where $currentUser = request_from OR $currentUser = request_to

This would return both columns and you would have to remove in PHP the current user.

Another way to get all friends from this schema is to run a UNION:

select request_from from friendships
where request_to = $currentUser
UNION
select request_to from friendships
where request_from = $currentUser

The drawback of this solution is that you're running 2 selects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文