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. EDIT Comments only have 1 tier of replies, ie replies CANNOT have replies. – Thanks 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?
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. (IE: I suck at mysql, but im learning 🙂
Since you said replies are one level deep..
I would make comments 1 table and have a
comment_idfield to denote ownership and anews_idfield to add the relationship to the news item. This way you can simply query for all comments that match thenews_idand sort it bycomment_id. And then a wee bit of PHP array magic will get you a sorted list of comments/replies.So having a look at your current table, you’re on the correct path.