格式JSON Postgresql

格式JSON Postgresql,json,postgresql,Json,Postgresql,我使用内部连接来连接3个表,所有者、商店和机器。 我试图查看多个表的JSON输出,如下所示: SELECT ow.*, st.*, ma.* FROM owner ow INNER JOIN st.store ON ow.OwnerId = st.OwnerId INNER JOIN machine ma ON ma.StoreId = st.StoreId; { "OwnerId": "1d2dd", "Name": "name test", "St

我使用内部连接来连接3个表,所有者、商店和机器。 我试图查看多个表的JSON输出,如下所示:

SELECT ow.*, st.*, ma.* 
FROM owner ow 
   INNER JOIN st.store ON ow.OwnerId = st.OwnerId 
   INNER JOIN machine ma ON ma.StoreId = st.StoreId;
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":[{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":[{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }]
        },
        {
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":[{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            },
            {
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }]
        }]
}
SELECT "owner"."id",
    json_agg(DISTINCT "store".*) AS "stores", 
    json_agg(DISTINCT "machine".*) AS "machines"
FROM "owners"
INNER JOIN "stores"
ON "stores"."ownerId" = "owners"."id"
INNER JOIN "machines"
ON "machines"."storeId" = "stores"."id"
WHERE "owner" = 1
GROUP BY "owner"."id";
我希望JSON格式如下:

SELECT ow.*, st.*, ma.* 
FROM owner ow 
   INNER JOIN st.store ON ow.OwnerId = st.OwnerId 
   INNER JOIN machine ma ON ma.StoreId = st.StoreId;
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":[{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":[{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }]
        },
        {
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":[{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            },
            {
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }]
        }]
}
SELECT "owner"."id",
    json_agg(DISTINCT "store".*) AS "stores", 
    json_agg(DISTINCT "machine".*) AS "machines"
FROM "owners"
INNER JOIN "stores"
ON "stores"."ownerId" = "owners"."id"
INNER JOIN "machines"
ON "machines"."storeId" = "stores"."id"
WHERE "owner" = 1
GROUP BY "owner"."id";
但是返回的JSON格式不是这样的 我使用的是PostgreSQL。

最简单(可能也是唯一明智的)方法是从表级别的单个记录构建JSON子文档,然后按层次将它们连接起来:

SELECT json_build_object('OwnerId', ownerid,
                         'Name', name,
                         'Store', stores)
FROM owner
JOIN (
    SELECT ownerid,
           json_agg(
               json_build_object('StoreId', storeid,
                                 'Name', name,
                                 'Code', code,
                                 'Machine', machines)) AS stores
    FROM store
    JOIN (
        SELECT storeid,
               json_agg(
                   json_build_object('MachineId', machineid,
                                     'Name', name,
                                     'Type', type)) AS machines
        FROM machine
        GROUP BY storeid) m USING (storeid)
    GROUP BY ownerid) s USING (ownerid);

输出不完全是我想要的,但它更好…这就是输出

[{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }
        }
},
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            }
        }

},
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":{
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }
        }
}]

它不会像数组一样连接来自同一存储区的机器,对于格式化为JSON的一对多关系,请尝试以下操作:

SELECT ow.*, st.*, ma.* 
FROM owner ow 
   INNER JOIN st.store ON ow.OwnerId = st.OwnerId 
   INNER JOIN machine ma ON ma.StoreId = st.StoreId;
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":[{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":[{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }]
        },
        {
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":[{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            },
            {
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }]
        }]
}
SELECT "owner"."id",
    json_agg(DISTINCT "store".*) AS "stores", 
    json_agg(DISTINCT "machine".*) AS "machines"
FROM "owners"
INNER JOIN "stores"
ON "stores"."ownerId" = "owners"."id"
INNER JOIN "machines"
ON "machines"."storeId" = "stores"."id"
WHERE "owner" = 1
GROUP BY "owner"."id";

使用哪种语言?简单的SQL select语句从不返回JSON,请查看Postgres中可用的JSON函数: