问题描述
我已经阅读了几个教程,关于mysql的文档,db结构,并且我通过php使用它几个星期。现在我得到一个问题,我不知道如何形成/组织/创建我的数据库结构的帖子和评论。我已经阅读了一些关于这一点(这里stackoverflow),但我没有找到任何有用的。我理解,我需要有2个表的帖子和评论,当我需要打印他们在页面上用外键(或ID)我合并(只在页面上,而不是SQL)。当一个人正在查看页面时,他正常地看到了帖子和注释,但在背景中,一切都存储在2个表中。
I've read several tutorials, documentations about mysql, db structures and also I'm using it via php for weeks. Now I get a problem, I don't know how to form/organize/create my db structure for posts and comments. I've already read some posts about this (here on stackoverflow), but I didn't find anything useful. I understand that I need to have 2 tables for posts and comments, and when I need to print them on the page with a foreign key (or ID) I "merge" them (only on the page, not with SQL). When a person is viewing the page, he is seeing the post and comments normally, but in the "background" everything is stored in 2 tables.
我需要添加
如果我的问题是真的,那意味着如果在一篇文章中有100多条评论,那意味着我需要每次ALTER表吗?这意味着如果发布A有3条评论,而发布B有150条评论,我的表评论将有100多列?
If my question is true, that means if in a post are 100+ comments, that means I need to ALTER the TABLE every single time? That means if post "A" has 3 comments and post "B" has 150 comments, my table "comments" will have 100+ columns?
E.g:
bla1 | bla2 | bla3 | - 空| - 空| ... | - empty - |
A | bla1 | bla2 | bla3 | - empty | - empty | ... | - empty - |
B | bla1 | bal2 | bla3 | bla4 | bla5 | bla6 | ... | bla100 |
B | bla1 | bal2 | bla3 | bla4 | bla5 | bla6 | ... | bla100 |
推荐答案
在基本层面,东西在你的应用程序。在这种情况下,邮政表和评论表。这么简单:
At a basic level, you would have a table for each type of "thing" in your application. In this case, a table for Posts and a table for Comments. Something as simple as this:
Post
--------
Id
Content
User
DatePosted
Comment
--------
Id
PostId
Content
User
DatePosted
这将创建一个一对多或者零到多,实际上)帖子和评论之间的关系,其中每个帖子可以具有零个或多个关联的元素,但是每个评论可以仅与一个帖子相关联。
This would create what's called a one-to-many (or zero-to-many, actually) relationship between Posts and Comments, whereby each Post can have zero or more associated Pomments but each Comment can be associated with only one Post.
在你的代码(这是一个完整的其他主题),要显示一个帖子及其相关的评论,有几个你可以做的事情。假设您输入了您想要的信息的 Id
,您可以获取该信息及其评论:
In your code (which is a whole other subject entirely), to display a Post and its associated Comments there are a couple of things you could do. Assuming you have, as input, the Id
of the Post you want, you can get that Post and its Comments:
SELECT `Content`, `User`, `DatePosted` FROM `Post` WHERE `Id` = ?Id
SELECT `Id`, `Content`, `User`, `DatePosted` FROM `Comment` WHERE `PostId` = ?Id
数据由您决定,以及您希望如何在应用程序中使用它。它将返回为两个表结果,前者有一个记录(如果Post存在),后者有零个或多个记录。当然,你可以在尝试使用它们之前检查它们是否存在等等(所以如果第一个查询没有返回任何结果,不要尝试继续显示Post,只是显示一个默认的响应或错误。)
What you do with that resulting data is up to you and how you want to use it in your application. It would come back as two table results, the former of which has one record (if the Post exists) and the latter of which has zero or more records. Naturally, you'll want to check that things exist before trying to use them, etc. (So if the first query returns no results, don't try to continue to display the Post. Just show a default response or an error.)
这篇关于mysql结构的帖子和评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!