Code Monkey home page Code Monkey logo

Comments (1)

digoal avatar digoal commented on May 23, 2024

create table posts (
id int primary key,
title text,
tags text,
body text,
parentid int
);

create table comments (
id int primary key,
postid int,
ans text
);

insert into posts values
(1,'标题1','标签1','问题1',null),
(2,'标题2','标签2','问题2',null),
(3,null,null,'问题1回答1',1),
(4,null,null,'问题1回答2',1),
(5,'标题3','标签3','问题3',null);

insert into comments values
(1,1,'问题1的留言1'),
(2,1,'问题1的留言2'),
(3,3,'问题1回答1的留言1'),
(4,4,'问题1回答2的留言1'),
(5,4,'问题1回答2的留言2');

create or replace function get_liuyan(int) returns text[] as $$
select array_agg(ans) from comments where postid=$1;
$$ language sql;

with recursive tmp as (
select id, title, tags, body, get_liuyan(ID) as liuyan, '' as huida, '{}'::text[] as huida_liuyan, 1 as level from
posts where parentid is null
union all
select posts.id, coalesce(posts.title,tmp.title), coalesce(posts.tags,tmp.tags), tmp.body, get_liuyan(posts.ID), posts.body, get_liuyan(posts.id) , tmp.level+1 from
tmp join posts on (posts.parentid=tmp.id)
)
select title,tags,body,max(liuyan) filter (where level=1),jsonb_object_agg(huida,huida_liuyan) filter (where level>1) from tmp
group by title,tags,body;

title | tags | body | max | jsonb_object_agg
-------+-------+-------+-----------------------------+-------------------------------------------------------------------------------------------------
标题3 | 标签3 | 问题3 | |
标题1 | 标签1 | 问题1 | {问题1的留言1,问题1的留言2} | {"问题1回答1": ["问题1回答1的留言1"], "问题1回答2": ["问题1回答2的留言1", "问题1回答2的留言2"]}
标题2 | 标签2 | 问题2 | |
(3 rows)

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.