【发布时间】: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