I have a small social network site which allows users to make comments on posts. I can display records based on the friends users have added using SQL. However, I want the comment made on each post to be shown directly below the post.
Post table:
all_id(PK), name, comment, time, expand
Comments table:
post_id, name, comment, all_id(FK)
I’m trying to replicate the functionality of Facebook: commenting on a post and seeing the post right below it. I have tried using a complex SQL join but it didn’t show any data.
While you could grab the post and all comments in a single query, the result set may be un-foreseeably large, in which case you may exhaust your database’s, your script’s or your server’s memory. But if you wish to do so, let me know and I can post an example.
Alternately, you could use two separate queries. The first would grab the post, the second would grab all comments relating to that post. Here is an example using PHP and mySQL: