【问题标题】:How to get an array subquery using postgress sql?如何使用 postgresql 获取数组子查询?
【发布时间】:2020-01-06 15:39:02
【问题描述】:

所以,我需要的是以下 json。

var plan = [{
  id: 11,
  title: 'give a title',
  actions: [
   {id: 1,
   planId: 11,
   title: 'give action name'},
   {
     id: 3,
     planId: 11,
     title: 'give another action name'
   }
  ]},
{
      id: 13,
      title: 'thirteen a title',
      actions: [
       {id: 1,
       planId: 13,
       title: 'thirteen action name'},
       {
         id: 3,
         planId: 13,
         title: 'thirteen another action name'
       }
      ]}
]

所以我有 2 个表格、计划和行动。两个表之间的关系是计划有很多动作。 计划(id,标题) Action(id, title, planId)

SELECT
*,
ARRAY (
    SELECT
        jsonb_build_object ('id',
            m.id,
            'title',
            m.title)
    FROM
        actions a
        INNER JOIN plan p ON p.id = a.planid
    ) AS actions
FROM
    plan

我不确定如何获得每个计划下的相关操作。

【问题讨论】:

    标签: sql arrays json postgresql


    【解决方案1】:

    这些查询可能是您正在寻找的:

    数据样本

    CREATE TEMPORARY TABLE plan (id INT, title TEXT);
    CREATE TEMPORARY TABLE action (id INT, title TEXT, planid INT);
    
    INSERT INTO plan VALUES (1,'plan a'),(2,'plan b');
    INSERT INTO action VALUES (1,'1st action plan a',1),
                              (2,'2nd action plan a',1),
                              (3,'1st action plan b',2);
    

    查询 - 多条 json 记录

    SELECT 
      json_build_object(
        'id',p.id,'title',p.title,
        'actions',(SELECT json_agg(row_to_json(t)) 
                   FROM (SELECT id,title,planid 
                   FROM action WHERE planid = p.id) t)) AS myjson
    FROM plan p;
                                                                            myjson                                                                     
    ------------------------------------------------------------------------------------------------------------------------------------------------
     {"id" : 1, "title" : "plan a", "actions" : [{"id":1,"title":"1st action plan a","planid":1}, {"id":2,"title":"2nd action plan a","planid":1}]}
     {"id" : 2, "title" : "plan b", "actions" : [{"id":3,"title":"1st action plan b","planid":2}]}
    (2 Zeilen)
    

    查询 - 单个 json 记录

    SELECT json_agg(row_to_json(myjson)) FROM 
    (SELECT 
      json_build_object(
        'id',p.id,'title',p.title,
        'actions',(SELECT json_agg(row_to_json(t)) 
                   FROM (SELECT id,title,planid 
                   FROM action WHERE planid = p.id) t)) as plan
    FROM plan p) myjson;
    
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     [{"plan":{"id" : 1, "title" : "plan a", "actions" : [{"id":1,"title":"1st action plan a","planid":1}, {"id":2,"title":"2nd action plan a","planid":1}]}}, {"plan":{"id" : 2, "title" : "plan b", "actions" : [{"id":3,"title":"1st action plan b","planid":2}]}}]
    (1 Zeile)
    

    【讨论】:

    • IMO array_to_json(array_agg(...)) 可以简化为 json_agg(...)
    • @Abelisto 你说得对……刚刚编辑了我的答案。谢谢!
    【解决方案2】:

    应该这样做:

    SELECT
    array_to_json(array_agg(row_to_json(tb_data))) AS data FROM 
    (
        SELECT
        tb_plan.id,
        tb_plan.titile,
        array_to_json(array_agg(row_to_json(tb_action))) AS actions
        FROM plan tb_plan
        INNER JOIN "action" tb_action ON tb_action.id = tb_action.id
        GROUP BY 1,2
    ) tb_data
    

    Here's the documentation 关于这个查询中使用的 postgresql 的 json 函数。

    【讨论】:

      【解决方案3】:

      这样的事情应该可以完成:

      SELECT json_agg(data)
      FROM
        (SELECT p.id,
                p.title,
                json_agg(a) FILTER (WHERE a.plan_id IS NOT NULL) AS actions
         FROM PLAN p
         LEFT JOIN action a ON a.plan_id = p.id
         GROUP BY p.id,
                  p.title) data;
      
      [
        {
          "id": 1,
          "title": "test plan 1",
          "actions": [
            {
              "id": 1,
              "title": "test action 1",
              "plan_id": 1
            },
            {
              "id": 2,
              "title": "test action 2",
              "plan_id": 1
            }
          ]
        },
        {
          "id": 2,
          "title": "test plan 2",
          "actions": [
            {
              "id": 3,
              "title": "test action 3",
              "plan_id": 2
            },
            {
              "id": 4,
              "title": "test action 4",
              "plan_id": 2
            }
          ]
        }
      ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-29
        • 1970-01-01
        • 2018-06-21
        相关资源
        最近更新 更多