简体   繁体   English

mysql查询从多个表中提取数据时遇到一些麻烦...逻辑问题

[英]Having some trouble with a mysql query pulling data from multiple tables… logic question

Ill try to keep this simple and to the point. 我会尽力保持这一点的简单性。 Essentially I have a news feed, and a comments section. 本质上,我有一个新闻提要和一个评论部分。 The comments section has two tiers: responses and then replies to responses. 评论部分分为两层:回复,然后回复回复。 Basically structured like so for a given news post: 对于给定的新闻帖子,基本结构如下:

 -> comment

 ---> reply

 ---> reply

Each comment can have multiple replies. 每个评论可以有多个回复。 Obviously, the WRONG way to do this is to do an SQL query for every comment to check for replies and list them out. 显然,执行此操作的错误方法是对每个注释进行SQL查询,以检查答复并将其列出。 EDIT Comments only have 1 tier of replies, ie replies CANNOT have replies. 编辑注释只有1层答复,即,答复不能有答复。 - Thanks JohnP -感谢JohnP

My Questions for this kind of query: 我对此类查询的疑问:

Should I keep the comments and replies in separate tables and use a JOIN, or can I keep the replies and comments in the same table and use a qualifier to separate the type? 我应该将评论和答复保留在单独的表中并使用JOIN,还是可以将答复和评论保留在同一表中并使用限定符来分隔类型?

Should I attempt to sort them using the query or pull all the data into an array and sort & display that way? 我应该尝试使用查询对它们进行排序还是将所有数据拉入数组并以这种方式进行排序和显示?

My table currently is as follow: 我的表目前如下:

ID (unique, auto increment)
NEWS_ID (ties the comment to a particular news post)
REPLY_ID (ties the comment to a parent comment if it is a reply to another comment)
USER_ID
BODY
PUBLISHED_DATE

Any suggestions from those wiser than me would be greatly appreciated! 那些比我更明智的建议将不胜感激! Im still in the very early stages of fully understanding JOINS and other higher level mysql query structures. 我仍然处于完全了解JOINS和其他更高级别的mysql查询结构的早期阶段。 (IE: I suck at mysql, but im learning :) (即:我在mysql上很烂,但是我正在学习:)

Since you said replies are one level deep.. 因为您说的是回覆,所以深一层。

I would make comments 1 table and have a comment_id field to denote ownership and a news_id field to add the relationship to the news item. 我将创建注释1表,并具有comment_id字段表示所有权,并具有news_id字段将关系添加到新闻项。 This way you can simply query for all comments that match the news_id and sort it by comment_id . 这样,您可以简单地查询与news_id匹配的所有评论,并按comment_id对其进行排序。 And then a wee bit of PHP array magic will get you a sorted list of comments/replies. 然后一点点的PHP数组魔术将为您提供评论/回复的排序列表。

So having a look at your current table, you're on the correct path. 因此,看看您的当前表,您就在正确的路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM