I want to learn the comment displaying algorithm behind Reddit. How is a comment related with its child and so on? How they are stored in the database?
Lets say
comment1
-comment2
--comment3
-comment4
--comment5
--comment6
---comment7
----comment8
comment9
How to display comment5 which is after comment4 which is after comment1? What is the idea behind this sequencing? And how to relate them in the database?
AS @Rafe said, the actual storage is pretty easy, it would be something like:
Of course actually getting information from this is (arguably) the hard part. You can of course get the children of a comment with something like:
SELECT * FROM table WHERE parent='4'will give you all the children of comment4. But counting children, listing all the children in hierarchical order would be a bit harder. Other answers may provide more information on that.