0

Given the following table, how can write a MySQL query to display threaded comments in a single page?

Table structure:

  • comment_id
  • comment_parent
  • comment_content

Thank you.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Psyche
  • 8,513
  • 20
  • 70
  • 85
  • 1
    see http://stackoverflow.com/questions/597882/how-do-i-implement-threaded-comments, http://stackoverflow.com/questions/1442993/how-to-build-threaded-comments-with-a-1-or-2-queries, http://stackoverflow.com/questions/1260878/mysql-structure-for-comments-and-comment-replies – ax. Nov 21 '09 at 15:29

3 Answers3

0
Select comment_content from table where comment_parent != 0;

Then use PHP to display the results however you want.

Jonathan
  • 1,735
  • 11
  • 17
  • Your question still isn't clear. I think what you mean to ask is how do you display threaded comments with PHP. The query to get all the comments will just be 'Select * from table'. Then you will need to write some PHP to display the comments in the correct order. So basically what you want to do is go through all the parent comments and if a comment has children go through and print those under the parent comment. If you allow for multilevel threading then it's somewhat more complicated. – Jonathan Nov 21 '09 at 16:28
  • 1
    I made a recursive function and everything is ok now. – Psyche Nov 21 '09 at 19:21
0

Depending on how deep you want to go ... but here is a 1-thread example to do with 1 query

SELECT * FROM `comment` a LEFT JOIN `comment` b ON a.comment_id = b.comment_id WHERE a.comment_parent = 0 

then use php to select wether the parent comment changes and display that.

but it would likely be better to do this over multiple queries for speed. you would need to do some benchmarks to be sure

Mike Valstar
  • 3,499
  • 5
  • 24
  • 32