title: Database schema design for posts, comments and replies
body: <p>In my previous project I had posts and comments as two tables:</p>
  
  <p>post</p>
  
  <ul>
  <li>id</li>
  <li>text</li>
  <li>timestamp</li>
  <li>userid</li>
  </ul>
  
  <p>comment</p>
  
  <ul>
  <li>id</li>
  <li>message</li>
  <li>timestamp</li>
  <li>userid</li>
  <li>postid</li>
  </ul>
  
  <p>Now I've got to design replies to comments. The replies is just one level, so users can only reply to comments, not to replies. The tree structure is only 1 level deep. My first idea was to use the same comment table for both comments and replies. I added a new column though:</p>
  
  <p>comment</p>
  
  <ul>
  <li>id</li>
  <li>message</li>
  <li>timestamp</li>
  <li>userid</li>
  <li>postid</li>
  <li>parentcommentid</li>
  </ul>
  
  <p>Replies have parentcommentid set to the parent comment they belong. Parent comments don't have it (null)</p>
  
  <p>Retrieving comments for a given post is simple:</p>
  
  <p>but this time I need another query to find out the comment replies. This has to be done for each comment:</p>
  
  <p>This doesn't seem to be a good solution, is there a way to have a single query which returns the complete list of comments/replies in the correct order? (dictated by the timestamp and the nesting)</p>