如何从PostgreSQL中检索json对象?

时间:2015-12-16 18:10:23

标签: sql json postgresql

在PostgreSQL 9.4中,我如何检索这样的json对象:

parentTableFirstProp: 'string',
parentToChildReference: [
    {childTableFirstProp: 'another string'},
    {childTableFirstProp: 'yet another string'}
}]

而不是:

[{
    parentTableFirstProp: 'string',
    childTableFirstProp: 'another string',
},{
    parentTableFirstProp: 'string',
    childTableFirstProp: 'yet another string'
}]

我是否总是必须进行2个选择查询,并通过使用别名来插入一个?

更新1

我尝试使用JOIN

SELECT
"public"."ParentTable".*,
"public"."ChildTable".*
FROM
"public"."ParentTable"
RIGHT JOIN "public"."ChildTable"
ON "public"."ParentTable"."childReference"

我将尝试解释ParentTableChildTable包含的内容:

ParentTable = {
    id: 111
    parentTableFirstProp: 'string',
    parentToChildReference: [222, 333]
}

ChildTable = [{
    id: 222,
    childTableFirstProp: 'another string'
},{
    id: 333,
    childTableFirstProp: 'yet another string'
}]

0 个答案:

没有答案
相关问题