简体   繁体   English

Postgres结果多维查询

[英]Postgres Result Multidimensional from Query

How would one create a Multidimensional Array from a single Postgres Query. 如何从单个Postgres查询创建多维数组。

I have 3 tables with the following columns: 我有3个表格,其中包含以下各列:

  • tb_school (id, school_name) eg: tb_school(id,school_name)例如:

    [ {id:"1", school_name:"School1"}, {id:"2", school_name:"School2"} ]

  • tb_profile (id, profile_name, school_id) eg: tb_profile(id,profile_name,school_id),例如:

    [ {id:"1", profile_name:"John", school_id:"1"}, {id:"2", profile_name:"Peter", school_id:"1"}, {id:"3", profile_name:"Sam", school_id:"1"}, {id:"4", profile_name:"Susan", school_id:"2"}, {id:"5", profile_name:"Jude", school_id:"2"}, {id:"6", profile_name:"Kim", school_id:"2"} ]

  • tb_article (id, article_name, profile_id) eg: tb_article(id,article_name,profile_id),例如:

    [ {id:"1", article_name:"Headline News", profile_id:"1"}, {id:"2", article_name:"Sports Recap", profile_id:"2"}, {id:"3", article_name:"Weather", profile_id:"3"}, {id:"4", article_name:"Arts", profile_id:"4"}, {id:"5", article_name:"Other", profile_id:"5"}, {id:"6", article_name:"Example", profile_id:"6"} ]

I would like the query to return a result lat looks something like this: 我希望查询返回结果纬度看起来像这样:

[
    {school_name:"School1", people:[
        {profile_name:"John", articles:[
            article_name:"Headline News"
        ]},
        {profile_name:"Peter", articles:[
            article_name:"Sports Recap"
        ]},
        {profile_name:"Sam", articles:[
            article_name:"Weather"
        ]},
    ]},
    {school_name:"School2", people:[
        {profile_name:"Susan", articles:[
            article_name:"Arts"
        ]},
        {profile_name:"Jude", articles:[
            article_name:"Other"
        ]},
        {profile_name:"Kim", articles:[
            article_name:"Example"
        ]},
    ]}
]

I know one can achieve this by 3 separate select queries in nested for loops. 我知道可以通过嵌套for循环中的3个独立的选择查询来实现此目的。 What I want to know is could this be achieved by one single postgres query? 我想知道的是,可以通过一个单一的postgres查询来实现吗?

Note: I am not good with PostgreSQL. 注意:我对PostgreSQL不好。 Probably made wrong assumptions and code is probably not intended for production. 可能做出错误的假设,并且代码可能不适用于生产。

I almost sure you cannot just result a nested object. 我几乎可以肯定,您不能仅得出嵌套对象。 But you can receive JSON. 但是您可以接收JSON。 So 所以

 SELECT json_build_object( 'school_name', school_name, 'people', ( SELECT json_agg(json_build_object( 'profile_name', profile_name, 'articles', ( SELECT json_agg(json_build_object( 'article_name', article_name )) FROM tb_article WHERE profile_id = tb_profile.facid ) )) FROM tb_profile WHERE school_id = tb_school.id ) ) AS result FROM tb_school 

it will give you row as JSON, which you can JSON.parse . 它将以JSON.parse

If you want to test you can use this learning service . 如果您想测试,可以使用此学习服务 I used it with this code. 我在这段代码中使用了它。

 SELECT json_build_object( 'name', firstname, 'slots', ( SELECT json_agg(json_build_object( 'number', slots, 'facility', ( SELECT json_agg(json_build_object( 'name', name )) FROM cd.facilities WHERE facid = cd.bookings.facid ) )) FROM cd.bookings WHERE memid = cd.members.memid ) ) AS result FROM cd.members 

It probably will be helpful or provide more ideas. 这可能会有所帮助或提供更多想法。

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

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