[英]Nested JSON from Postgres API, schema does not exist?
I have a SQL API to my Postgres database that returns JSON. 我对Postgres数据库有一个返回JSON的SQL API。
I have two tables: holidays
, which has a schema of name
, hd_id
. 我有两个表: holidays
,其name
hd_id
。 And photos
, which are photos take on that day. 还有photos
,这是当天拍摄的照片。 Its schema is url
, caption
, h_id
. 其模式为url
, caption
, h_id
。
I'd like to create a nested json object like the one down below. 我想创建一个嵌套的json对象,如下所示。 The SQL I'm running is 我正在运行的SQL
SELECT holidays.name, holidays.h_id,
concat('[', group_concat(concat('{"src":"', photos.url, '","caption":"', photos.caption '"}', separater ','), ']') )
FROM holidays
INNER JOIN photos
ON holidays.h_id = photos.h_id
GROUP BY holidays.h_id
But that gives me the error "schema "photos" does not exist"
. 但这给了我错误"schema "photos" does not exist"
。 Photos is a table, not a schema. 照片是一个表,而不是一个架构。 I don't seem to be making the same mistake as this seemingly related question . 我似乎并没有和这个看似相关的问题犯同样的错误。 I'm not sure how else to structure the JOIN. 我不确定该如何构造JOIN。
Here's the desired JSON output. 这是所需的JSON输出。
[
{
name: 'Labor Day',
h_id: 'a1',
photos: [
{
src: 'url',
caption: 'text'
},
{
src: 'url',
caption: 'text'
}
]
},
{
name: 'MLK Day',
h_id: 'a2',
photos: [
{
src: 'url',
caption: 'text'
},
{
src: 'url',
caption: 'text'
}
]
}
]
There's no group_concat
in PostgreSQL
. PostgreSQL
没有group_concat
。 You may use string_agg : 您可以使用string_agg :
select
h.name, h.h_id,
'[' || string_agg('{"src":"' || p.url || '", "caption":"' || p.caption || '"}', ',') || ']'
from holidays as h
inner join photos as p on h.h_id = p.h_id
group by h.name, h.h_id
see sql fiddle demo with this example 参见sql fiddle演示与该示例
Or use JSON functions . 或使用JSON函数 。 There's also nice JSON support in version 9.3 9.3版还提供了不错的JSON支持
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.