我正在尝试查询以获取与该报告相关联的report
和所有report_items
。
reports
表结构:id |名称| creation_date
report_items
表结构:id | report_id |地方制片人|序列号
我尝试过:
SELECT "reports".*,
to_json("report_items".*) as "items"
FROM "reports" INNER JOIN "report_items" USING ("id")
WHERE "reports".id = ${req.params.id}
但只有第一个report_item返回(而不是report_items列表):
{"id":1,"type":"fd","name":"dsfdsfds","client_name":"fdsfds","website":"dsffds","creation_time":"2019-03-12T22:00:00.000Z","items":{"id":1,"report_id":1,"place":"saddsa","type":"sdadsa","producer":"sdadsa","serial_number":"adsdsa","next_check_date":"2019-03-19","test_result":"saddsa","comments":"saddsa"}}
预期结果:
{"id":1,"type":"fd","name":"dsfdsfds","client_name":"fdsfds","website":"dsffds","creation_time":"2019-03-12T22:00:00.000Z","items": [{"id":1,"report_id":1,"place":"saddsa","type":"sdadsa","producer":"sdadsa","serial_number":"adsdsa","next_check_date":"2019-03-19","test_result":"saddsa","comments":"saddsa"}, {"id":1,"report_id":1,"place":"saddsa","type":"sdadsa","producer":"sdadsa","serial_number":"adsdsa","next_check_date":"2019-03-19","test_result":"saddsa","comments":"saddsa"}]}
知道我缺少什么吗?
我可以做的一个很长的解决方案(肯定不是很理想。):
SELECT * FROM reports
WHERE id = ${req.params.id}
SELECT * FROM report_items
WHERE report_id = ${req.params.id}
并通过编程将它们组合在一起。
答案 0 :(得分:0)
如果我对问题的理解正确,那么期望的结果是将报告数据包含一行作为JSON数组的所有内容。
在此假设下,以下查询应起作用:
WITH data (id, items) AS (
SELECT report_id, array_to_json(array_agg(to_json(*)))
FROM report_items WHERE report_id = ${req.params.id}
GROUP BY report_id
)
SELECT reports.*, data.items
FROM reports
INNER JOIN data ON (reports.id = data.report_id)
WHERE reports.id = ${req.params.id}
...或作为子选择(因为PostgreSQL不够聪明,无法将过滤器推入CTE:
SELECT reports.*, data.items
FROM reports
INNER JOIN (SELECT report_id, array_to_json(array_agg(to_json(report_items.*)))
FROM report_items
GROUP BY report_id) AS data (id, items) ON (reports.id = data.id)
WHERE reports.id = ${req.params.id}
在两种情况下,输出列items
将包含所有相关项目的JSON数组。
如果您希望将完整的报告条目作为JSON,请尝试以下操作(请注意,此操作仅适用于jsonb
,不适用于json
):
SELECT jsonb_set(to_jsonb(reports.*), '{items}', data.items, true) AS report_data
FROM reports
INNER JOIN (SELECT report_id, array_to_json(array_agg(to_json(report_items.*)))::jsonb
FROM report_items
GROUP BY report_id) AS data (id, items) ON (reports.id = data.id)
WHERE reports.id = ${req.params.id}