I want to return the loop of all the comments where a comment is a comment-of an especific post.
For example in the next page post?id=30
I want to show all the loop of comment till getting to the main post...does it makes sense?
Posts
id_post | post | comment_of
25 main post 0
26 comment 1st level 25
27 comment 2nd level 26
28 another post 0
29 comment not related 14
30 comment 3rd level 27
31 comment not related 13
Query
SELECT id_post,
post,
comment_of
FROM (SELECT * FROM posts
ORDER BY comment_of, id_post) posts_sorted,
(SELECT @pv := '25') initialisation
WHERE find_in_set(comment_of, @pv)
AND length(@pv := concat(@pv, ',', id_post)) /*key line*/
OR id_post=@pv
Result
id_post | post | comment_of
25 main post 0
26 comment 1st level 25
27 comment 2nd level 26
30 comment 3rd level 27
working fiddle http://sqlfiddle.com/#!9/c07667/2
PROBLEM
With this fiddle I go from the main post to the last comment....What I want is starting with the last comment (id_post=30)
to get to the main post (id unknown) in page post=?id=30