3

I have to store blog style comments in a database. Each comment can have a parent one.

I am doing it with a column called "parentComment" that has a null value when it is a top level comment or have a value if it is a response to another comment.

What is the most efficient way to retrieve a comment and all its childs?

Santiago Corredoira
  • 47,267
  • 10
  • 52
  • 56
  • On which DBMS? They differ in how they do hierarchical queries. – Tony Andrews Dec 11 '08 at 17:30
  • See http://stackoverflow.com/questions/192220/what-is-the-most-efficientelegant-way-to-parse-a-flat-table-into-a-tree – Bill Karwin Dec 11 '08 at 17:30
  • I agree with Dawkins, but, anyway, here is another similar question: http://stackoverflow.com/questions/38801/sql-how-to-store-and-navigate-hierarchies – jm. Jan 12 '09 at 06:02

1 Answers1

1

A quite common technique is to also have a (duplicate, indirectly) relation to the "root" of the tree, which means you can select the full tree in one neat select. Othewise it quickly gets dirty.

krosenvold
  • 75,535
  • 32
  • 152
  • 208