检索父sql,mysql的循环注释

时间:2018-03-19 19:15:59

标签: mysql sql

我想返回所有评论的循环,其中评论是特定帖子的评论。

例如在下一页post?id=30中,我想显示所有评论循环,直到到达主帖...这有意义吗?

帖子

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 

查询

    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 

结果

id_post     |       post          | comment_of 
    25          main post               0 
    26          comment 1st level       25 
    27          comment 2nd level       26 
    30          comment 3rd level       27 

工作小提琴http://sqlfiddle.com/#!9/c07667/2

问题

有了这个小提琴,我会从主帖到最后一条评论....我想要的是从最后一条评论(id_post=30)开始到达{{1}页的主要帖子(身份未知) }

0 个答案:

没有答案