Php 创建要存储的SQL好友表
我自己创建了一个网站,允许用户彼此添加好友,并对彼此的帖子发表评论。在研究了这一点之后,我不确定该如何开始。我有一个posts和一个users表 我的问题是如何将数据库与 1.检查用户A是否已向用户B发送好友请求? 2.存储一个值以指示他们是朋友Php 创建要存储的SQL好友表,php,mysql,database,database-design,Php,Mysql,Database,Database Design,我自己创建了一个网站,允许用户彼此添加好友,并对彼此的帖子发表评论。在研究了这一点之后,我不确定该如何开始。我有一个posts和一个users表 我的问题是如何将数据库与 1.检查用户A是否已向用户B发送好友请求? 2.存储一个值以指示他们是朋友 在发布之前,我已经浏览了这个网站,但似乎不知道如何实现这一点。我很确定我把事情复杂化了。有人能解释一下这个概念或者它是如何工作的吗?这可以通过关系数据库来完成,值得一读关系数据库是如何工作的 但是-为了给出一些提示,您似乎希望您的关系数据库允许以下功能
在发布之前,我已经浏览了这个网站,但似乎不知道如何实现这一点。我很确定我把事情复杂化了。有人能解释一下这个概念或者它是如何工作的吗?这可以通过关系数据库来完成,值得一读关系数据库是如何工作的 但是-为了给出一些提示,您似乎希望您的关系数据库允许以下功能:
- 许多用户
- 由某些用户发布的一系列帖子
- 用户之间的许多关系
- 在确认关系之前验证关系的能力
- 由某些用户发布的一系列帖子
- 关于帖子的一些“子帖子”评论
users
表:
UserID, name, age, [details, password, address etc etc]
然后是一个包含以下字段的friends
表:
friendID, userID1, userID2, [date, confirmed]
您的用户表可能如下所示:
UserID, name, age,
1 Fred 18
2 George 24
3 Michael 20
4 Alice 24
5 Sophie 20
6 George 19
commentid, postid, userid content
1 1 3 (Michael) Michael is commenting on his own first post...
2 2 4 (Alice) Alice is saying something on Michael's second post
假设Michael想与Alice和Fred成为朋友,Alice想与Sophie成为朋友-您想在friends表中创建如下记录:
FriendID, userID1, userID2,
1 3 (this refers to Michael) 4 (this refers to Alice)
2 3 (this refers to Michael) 1 (this refers to Fred)
3 4 (this refers to Alice) 5 (this refers to Sophie)
postid, userid, date content
1 3 (Michael) [auto datetime] Hi everyone
2 3 (Michael) [auto datetime] This is my second post
因此,如果你接着寻找迈克尔的朋友,你会做一个查询,寻找:
every record from the friend table where userID1 = Michael's userID.
From the userID2 field, you'd get userID 4 and userID 1
By looking up those userids in the user table, you'd find more details for Alice and Fred.
您应该进行此查询,检查userID1或userid2=您需要的userID,以便在表格外观稍有不同的情况下获得相同的结果:
FriendID, userID1, userID2,
1 3 (this refers to Michael) 4 (this refers to Alice)
2 1 (this refers to Fred) 3 (this refers to Michael)
3 4 (this refers to Alice) 5 (this refers to Sophie)
Otherwise you'd only know about Alice.. but you want to know about Fred too.
2) 如果要确认关系,可以在friends表中添加一个“已确认”字段-将其设置为二进制0=未确认/1=已确认
当请求友谊时,您会将记录添加到表中,但当确认后,您会将该记录的“已确认”字段更新为1
让我们相应地更新我们的朋友表:
FriendID, userID1, userID2, confirmed
1 3 (Michael) 4 (Alice) 0
2 3 (Michael) 1 (Fred) 1
3 4 (Alice) 5 (Sophie) 1
如果您想查看所有等待Michael接受的朋友,请搜索:
any records from the friends table where userid1 = 3
AND confirmed = 0 ... which means it hasn't been accepted yet.
这表明alice还没有被Michael接受为朋友
如果您想查看用户请求但未被接受的所有好友,请查找:
any records from the friends table where userid2 = the user you're looking for
AND confirmed = 0 ... which means it hasn't been accepted yet.
如果要查看所有已接受的好友,请将“已确认”切换为1
3) 您还希望每个用户都有帖子。。。因此,您需要一个带有以下字段的posts
表:
postid, userid, date, content
我们已经有了你的用户表,所以假设Michael想发布一些东西。posts表可能如下所示:
FriendID, userID1, userID2,
1 3 (this refers to Michael) 4 (this refers to Alice)
2 3 (this refers to Michael) 1 (this refers to Fred)
3 4 (this refers to Alice) 5 (this refers to Sophie)
postid, userid, date content
1 3 (Michael) [auto datetime] Hi everyone
2 3 (Michael) [auto datetime] This is my second post
现在,Michael和posts表之间有了关系。如果另一个用户发布了一些东西,他们会用不同的用户ID添加另一行。然后可以从posts表中检索所有posts,其中userid=3,这是Michael的userid
4) 要在帖子上添加评论,您需要一个如下所示的评论表:
UserID, name, age,
1 Fred 18
2 George 24
3 Michael 20
4 Alice 24
5 Sophie 20
6 George 19
commentid, postid, userid content
1 1 3 (Michael) Michael is commenting on his own first post...
2 2 4 (Alice) Alice is saying something on Michael's second post
感谢您的深入回复。这个回答正是我想要的。对不起,问题太宽泛了。有时候我很难解释。如果你想找苏菲的朋友怎么办?当您的sql应该返回4感谢您指出这一点时,它不会返回任何内容。我已经编辑了答案。