I'm trying to build a small blog allowing people to add comments..
For comments I prepared a table like this:
ID PostID ParentID Date Title Text User ....
1 1 0
2 2 0
3 1 0
4 1 1
5 1 1
6 1 4
7 1 1
8 1 5
9 1 8
where if ParentID
is not 0 means that this comment is not referred to the post but to the comment with that ID (is nested)
ie: assuming to select Post 1 will extract all comments except ID=2.
- we have posts 1 and 2 on the root
- posts 4,5,7 are nested in comment 1
- post 6 is nested in 4 (triple nesting)
- post 8 is nested in 5 (triple nesting)
- post 9 is nested in 8 (quadruple nesting)
Therefore I expect the query returns the record in this order:
1
4
6
5
8
9
7
2
But I'm struggling with the query to achieve the above result.
Actually, for single nesting I find a solution, but for deeper nesting.. have really no idea..
I also thought that table schema used is not adequate..
On the other side I thought that such issue can be a common issue, but google didn't gave me any valuable result.. probably used wrong keywords..
Can you please suggest a path to solve this problem, or where to search for?
Thanks!
Jan