我一直在试图弄清楚如何做到这一点,即使查看其他示例,我也无法弄清楚,所以也许我可以获得一些个性化的帮助。
我有两张桌子users_status
and friendships
.
In the users_status
表我有一个字段userid
,以及其他几个。
在里面friendships
表,我有字段request_to
,request_from
, and friendship_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_requests
and friendships
?如果是这样,我将如何构建/管理friendships
table?
您可以使用表连接(例如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)
replace $userid
与您的用户 ID
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)