use*_*810 -3 php mysql database database-design
我自己创建了一个网站,允许用户互相添加为朋友,并对彼此的帖子发表评论.在调查之后,我不确定如何开始.我有一个帖子和一个用户表.
我的问题是如何将数据库与1.关联.检查用户A是否向用户B发送了朋友请求?2.存储一个值,表明他们是朋友?
我在发布之前浏览了这个网站,但似乎无法理解如何进行此操作.很确定我只是让它复杂化了.任何人都可以解释这个概念或如何运作?
这可能与关系数据库来完成,这将是值得如何关系型数据库的工作如读了https://www.youtube.com/watch?v=NvrpuBAMddw
但是 - 为了给出一些指示,看起来你希望你的关系数据库允许以下函数:
1)让我们从"朋友请求"部分开始.
这需要你拥有a)不同用户的整个负载,以及b)这些用户之间的关系负载.
您需要在2个不同的表中表示 - 所以创建一个users包含以下字段的表:
UserID, name, age, [details, password, address etc etc]
Run Code Online (Sandbox Code Playgroud)
然后是一个friends包含以下字段的表:
friendID, userID1, userID2, [date, confirmed]
Run Code Online (Sandbox Code Playgroud)
您的用户表可能如下所示:
UserID, name, age,
1 Fred 18
2 George 24
3 Michael 20
4 Alice 24
5 Sophie 20
6 George 19
Run Code Online (Sandbox Code Playgroud)
让我们说迈克尔希望成为爱丽丝和弗雷德的朋友,爱丽丝希望与索菲成为朋友 - 你想在你的朋友表中创建如下所示的记录:
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)
Run Code Online (Sandbox Code Playgroud)
所以如果你找了迈克尔的朋友 - 你会做一个寻找的查询:
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.
Run Code Online (Sandbox Code Playgroud)
您应该使此查询检查userID1 或 userid2 是否是您需要的用户ID,以便在表格看起来略有不同时获得相同的结果:
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.
Run Code Online (Sandbox Code Playgroud)
2)如果要确认关系,可以在"朋友"表中添加"已确认"字段 - 将其设置为二进制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
Run Code Online (Sandbox Code Playgroud)
如果你想看到所有正在等待Michael接受的朋友,你会搜索:
any records from the friends table where userid1 = 3
AND confirmed = 0 ... which means it hasn't been accepted yet.
Run Code Online (Sandbox Code Playgroud)
这表明爱丽丝尚未被迈克尔接受为朋友.
如果您想查看用户已请求的所有朋友,但尚未接受,请查找:
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.
Run Code Online (Sandbox Code Playgroud)
如果您想查看所有已接受的朋友,请将"已确认"切换为1.
3)您还想为每个用户发帖...所以您需要一个posts包含以下字段的表:
postid, userid, date, content
Run Code Online (Sandbox Code Playgroud)
我们已经有了你的用户表,所以让我们说迈克尔想发布一些东西.posts表可能如下所示:
postid, userid, date content
1 3 (Michael) [auto datetime] Hi everyone
2 3 (Michael) [auto datetime] This is my second post
Run Code Online (Sandbox Code Playgroud)
你现在已经得到了Michael和posts表之间的关系.如果另一个用户发布了某些内容,他们会添加另一个具有不同用户ID的行.然后,您可以从posts表中检索userid = 3的所有帖子,这是Michael的用户ID.
4)要在帖子上添加评论,您需要一个可能如下所示的评论表:
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
Run Code Online (Sandbox Code Playgroud)