简体   繁体   English

如何查询postgres以json格式获取数据?

[英]How to query postgres to get data in json format?

在此处输入图片说明

From the table I want to select users firstname and lastname only if their age is greater than 10 in a json format. 我要从表中选择仅以json格式年龄超过10岁的用户的名字和姓氏。

So the output of the query should be 因此查询的输出应为

在此处输入图片说明

I could select create a json text from but not sure how to apply where clause for Key = 'AGE' and value > 10 我可以选择从中创建一个json文本,但不确定如何为Key = 'AGE' and value > 10应用where子句

Here is my query 这是我的查询

select json_object_agg(key,value) from table where key in ('firstname','lastname')

Add an aggregate with a FILTER clause: 添加带有FILTER子句的聚合:

SELECT id,
       json_object_agg(key, value) AS json,
FROM mytable
GROUP BY id
HAVING (max(value::integer) FILTER (WHERE key = 'AGE')) > 10;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM