postgresql - How to create nested comment and reply tree with a common table expression -


i thought had solved this, alas haven't.

i looking postgresql pump out comments , replies in order can rendered client side no additional sort.

i have following common table expression have tried multiple things have yet output need.

with recursive comment_tree ( select      id           cte_id,      reply_to     cte_reply_to,      "createdat"  cte_date,      body         cte_body,      commenter_id cte_commenter_id,      1            level,      array[0]     sort,      article_id  "comments"  reply_to null  union  select      c.id,      c.reply_to,      c."createdat",      c.body,      c.commenter_id,      p.level + 1     level,      p.sort || p.level,      c.article_id  "comments" c  join comment_tree p on c.reply_to = p.cte_id)  select      sort,     cte_body         body,      cte_id           id,      cte_reply_to     reply_to,      cte_date         "createdat",      cte_commenter_id commenter_id,      level  comment_tree  order sort 

the result of follows.

enter image description here

what need

enter image description here

how can sort reproduce manually created body column?


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -