【发布时间】:2022-01-27 22:18:00
【问题描述】:
Postgres V13
试图了解如何在 PS(简单表)中生成 Json 基础关系数据, 我正在编写一个生成 SQL 的工具,用于基于关系数据生成 Json, 用户提供映射规则 - 将每个 Json 字段映射到 table.field 和工具构建 SQL 以提供所需的 json 格式的数据。
例如对于我需要在 Ps 上创建的非常相似的 json, 在 Oracle 数据库上(Oracle 版本 >12.2) 我通过运行得到它:
SELECT JSON_OBJECT ('FIRST_NAME' VALUE TO_CHAR(CUSTOMER.FIRST_NAME),
'concatDeails' VALUE JSON_OBJECT ('conctant_name' VALUE TO_CHAR(CUSTOMER.FIRST_NAME) || TO_CHAR(CUSTOMER.LAST_NAME)),
'Payments' VALUE (SELECT JSON_ARRAYAGG ( JSON_OBJECT ('payment_id' VALUE PAYMENT.payment_id,
'amount' VALUE payment.amount
)
RETURNING VARCHAR2(32000) )
FROM PAYMENT WHERE PAYMENT.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID )format json ) JSON_OUT
FROM CUSTOMER
而且有一个--> 客户与付款表之间的许多关系, 所以对于 Postgres,我有 3 个表: 客户(address_id 是唯一键) 地址(address_id 为 pk) 付款(客户可以多次付款)
json 应该如下所示:
{
"first_name": "Jared",
"last_name": "Ely",
"concatDeails":{ "conctant_name": "Jared Ely"},
"Address":{"city": "NY" ,"Zip": 123123},
"Payments":[{"payment_id": 1,"amount":100 , "credit": null }]
}
我知道如何创建每个块,但出于某种原因, 无法在单个查询中获取所有这些。
用于获取 first_name 和 concatDeails:
select json_build_object('first_name' , customer.first_name ,'concatDeails' ,json_build_object('last_nam1e' , customer.last_name||customer.last_name))
from customer
地址字典:
select
json_build_object('address' ,json_build_object('city_id' , address.city_id, 'postal_code' ,address.postal_code) )
from address ;
支付列表对象:
select jsonb_build_array(json_build_object('payment_id' , payment.payment_id ,'amount' , payment.amount ,"credit" , payment.credit)) from payment ;
当我尝试将它们组合成单个查询时, 失败:
elect json_build_object('first_name' , customer.first_name ,'concatDeails' ,json_build_object('last_nam1e' , customer.last_name||customer.last_name),
(select json_build_object('address' ,json_build_object('city_id' , address.city_id, 'postal_code' ,address.postal_code) )
from address where address.address_id=customer.address_id),
(select jsonb_build_array(json_build_object('payment_id' , payment.payment_id ,'amount' , payment.amount))
from payment where payment.customer_id=customer.customer_id)
)
from customer;
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
实际上,即使我尝试将地址和客户结合起来,我也会遇到错误: https://sqlize.online/sql/psql13/c495c4468aa8a9af897f28c5762c9035/
能否解释一下我如何将它们全部组合为单个查询 以最简单的方式? 第二,如何在没有返回值的json中显示“null”(参见示例Paymnet-->credit)?
【问题讨论】:
-
请提供您的表格结构和示例数据minimal reproducible example
-
我的意思是因为您有不止一笔客户付款
标签: sql postgresql