How do I SELECT data from child table in PostgreSQL?

后端 未结 1 575
旧时难觅i
旧时难觅i 2021-01-24 21:33

In PostgreSQL 9.4, how do I retrieve json object like this:

parentTableFirstProp: \'string\',
parentToChildReference: [
    {childTableFirstProp: \'another strin         


        
1条回答
  •  隐瞒了意图╮
    2021-01-24 21:55

    A new feature in PostgreSQL 9.2 but I didn't test the query:

    I follow the tutorial from here.

    select row_to_json(t)
    from (
    select ParentTable.parentTableFirstProp, (
    select array_to_json(array_agg(row_to_json(child)))
      from (
        select childTableFirstProp
        from ChildTable
        where ChildTable.id=ParentTable.parentToChildReference
      ) child
    
      ) as parentToChildReference
    from ParentTable
    ) t
    

    0 讨论(0)
提交回复
热议问题