我有一个问题:
SELECT
row_to_json(( SELECT s FROM (
SELECT
id,
addr_id,
target,
status
) s
)) FROM shop;
返回我这样的行:
{"id": 1, "addr_id": 12, "target": null, "status": "ACTIVE"}
如何重写我的查询,因此row(addr_id)的值将是我的json的关键:
{12: {"id": 1, "target": null, "status": "ACTIVE"}}
答案 0 :(得分:3)
使用json_build_object
代替row_to_json
。我认为这也使查询更具可读性。
SELECT
json_build_object(shop.addr_id, json_build_object(
'id', shop.id,
'target', shop.target,
'status', shop.status
))
FROM shop;
答案 1 :(得分:2)
您可以使用json_build_object
。
SELECT
json_build_object(addr_id,
row_to_json(( SELECT s FROM (
SELECT
id,
target,
status
) s
))) FROM shop;