如何使用PostgreSQL获取数组子查询?

huangapple go评论84阅读模式
英文:

How to get an array subquery using postgress sql?

问题

以下是翻译后的代码部分:

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

var plan = [{
  id: 11,
  title: '给一个标题',
  actions: [
   {id: 1,
   planId: 11,
   title: '给予动作名称'},
   {
     id: 3,
     planId: 11,
     title: '给予另一个动作名称'
   }
  ]},
{
      id: 13,
      title: '十三个标题',
      actions: [
       {id: 1,
       planId: 13,
       title: '十三个动作名称'},
       {
         id: 3,
         planId: 13,
         title: '给予另一个动作名称'
       }
      ]}
]

我已经提供了您所需的翻译,没有其他内容。

英文:

So, what I need is a following 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'
       }
      ]}
]

SO I have 2 tables, plan and actions. The relation between two table is Plan has many actions.
Plan(id, titile)
Action(id, titile, 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

I'm not sure how can I get the related actions under each plan.

答案1

得分: 2

以下是翻译好的部分:

Data sample

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);

Query - multiple json records

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;

Query - single json record

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;

请注意,已经去除了HTML编码的转义字符。

英文:

These queries might be what you're looking for:

Data sample

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);

Query - multiple json records

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)

Query - single json record

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)

答案2

得分: 1

这应该可以:

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

这里是关于在此查询中使用的PostgreSQL的JSON函数的文档

英文:

This should do it:

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

Here's the documentation about the postgresql's json functions used in this query.

答案3

得分: 0

以下是翻译好的代码部分:

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
      }
    ]
  }
]
英文:

Something like this should do the job:

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
      }
    ]
  }
]

huangapple
  • 本文由 发表于 2020年1月6日 23:39:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定