title: How to sort out comments on comments in php
body: <p>I want to provide my website visitors the facility to post comment on an existing comment, as we can post comment on an answer in stackoverflow.</p>
  
  <p>Here I have created two tables</p>
  
  <pre><code>threads
      thread_id (auto increment),
      title VARCHAR(MAX),
      body VARCHAR(MAX),
      date datetime,
      user INT
  
  comments
      comment_id INT (auto increment),
      parent_id INT,
      thread_id INT,
      title VARCHAR(MAX),
      body VARCHAR(MAX),
      date datetime,
      user INT
  </code></pre>
  
  <p>When User will Post the Comment on a thread it will be saved as below in comments table</p>
  
  <pre><code>comment_id = 1211
  parent_id = NULL
  thread_id = 122
  title = "This is the title of the comment"
  body = "This is the Body of the comment"
  date = 2013-04-04 13:05:44
  user = "xyzuser"
  </code></pre>
  
  <p>suppose a user post a comment on the above mentioned comment, that comment will be saved as below in comments table</p>
  
  <pre><code>comment_id = 1212
  parent_id = 1211
  thread_id = 122
  title = "This is sample title of comment on comment"
  body = "This is the Body of comment on comment";
  date = 2013-04-04 15:05:44
  user = "abcuser"
  </code></pre>
  
  <p>I am using the following query to fetch a thread from threads table</p>
  
  <pre><code>SELECT * FROM threads WHERE thread_id = 122
  </code></pre>
  
  <p>Till Now, I am getting the comments under the thread from the comments table as below</p>
  
  <pre><code>SELECT * FROM comments WHERE thread_id = 122
  </code></pre>
  
  <p>But Now, I also want to show the comments under each comment, 
  I have tried following</p>
  
  <pre><code>SELECT * FROM comments WHERE thread_id = 122 GROUP BY parent_id
  </code></pre>
  
  <p>But using this query what will happen to the rows having NULL value in parent_id, and How I will sort out the comments under each comment.</p>
  
  <p>Any One has any solution?? Which Query Should I use and How to sort out the comments under each comment??</p>