问题描述
我一直试图找出如何做到这一点,甚至在看其他例子的时候,我无法弄清楚,所以也许我可以得到一些个性化的帮助。
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.
我有两张表, users_status
和 friendships
。
在 users_status
表中,我有一个字段 userid
和其他几个。
在 friendships
表中,我有字段 request_to
, request_from
和 friendship_status
。
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
.
基本上我想做的是获取所有状态信息由当前用户和当前用户的朋友(我可以在PHP中使用$ userid变量指定)。
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).
这里是一个例子, c $ c> friendships 表结构。当发送好友请求时,发件人和接收者的用户名被放置在表中,其中friend_status为0.当请求被接受时,将将友谊状态设置为1,然后将这两个当成是朋友。
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.
对于 friend_requests
和 friendships
两个单独的表可能会更好吗?如果是这样,我如何构建/管理友谊
表?
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?
推荐答案
您可以使用表格连接(例如)查找所有请求。
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.
其实你可以在这里使用一个子查询: p>
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)
将 $ userid
替换为您的用户ID
replace $userid
with your user id
这篇关于如何建立友谊关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!