Comments (1)
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)
- 请教pgsql的json解析类问题 HOT 1
- 请教一个有关pg_class被vacuum full的问题 HOT 2
- 關於 postgresql page size 疑問 HOT 4
- wsl2 run docker 跑 PG DB 並 使用 stap HOT 2
- 是否有方法將 Postgresql DB 複製統計資訊並匯入其他 Postgresql DB 裡? HOT 2
- Unique index 如何證明效能比 non Unique index 佳? HOT 2
- 许愿
- 修改storage_parameter参数问题 HOT 1
- circle的半径 1000和米应该怎样换算呢
- 关于pg 的test case HOT 1
- 如何实现Mysql作为OLTP,PG作为OLAP HOT 3
- pg按任意字段搜索不支持排序 HOT 1
- 德哥,为什么现在很少更新pg的东西了 HOT 6
- 关于《德说-第147期, 如何把100块钱变成1万块钱?》 HOT 1
- 基于PostgreSQL创业的公司有那些 HOT 2
- 请问德哥,PG有类似canal的数据同步中间件吗? HOT 1
- 《PostgreSQL+PolarDB开源数据库人才认证培训》实验手册持续建设 HOT 3
- please remove this git with leaked credentials HOT 1
- limit 1 导致查询变慢 HOT 1
- 德哥,咨询一个权限问题,涉及到不同模式和视图 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.