【问题标题】:Postgres jsonb_insert arrayPostgres jsonb_insert 数组
【发布时间】:2021-01-14 13:50:52
【问题描述】:

我正在尝试构建一个查询,该查询返回一组客户购物车及其所有商品,例如

表格:

购物车idcustomer_id

项目cart_idpricebrand

所需的 JSON 结果示例:

[{
  "id": 1, "customer_id": 1,
  "items": [{
    "price": 14.50, "brand": "Coke"
  }]
}]

失败的 SQL 查询(可在此处测试:http://sqlfiddle.com/#!17/0ef54/1):

SELECT jsonb_insert(to_jsonb(c.*), 'items', array(
  SELECT to_jsonb(i.*)
  FROM item i
  WHERE i.cart_id = c.id
)
FROM cart c

如何做到这一点?

【问题讨论】:

    标签: postgresql postgresql-json


    【解决方案1】:

    听起来好像您正在寻找聚合:

    select jsonb_agg(to_jsonb(c)||jsonb_build_object('items', i.items))
    from cart c 
      join (
        select cart_id, jsonb_agg(to_jsonb(i) - 'cart_id') as items
        from item i
        group by cart_id
      ) i on i.cart_id = c.id;
    

    Online example

    【讨论】:

    【解决方案2】:
    select jsonb_agg(to_jsonb(c.*) || 
     jsonb_build_object
     (
      'items', 
      (select jsonb_agg(to_jsonb(i.*) - 'cart_id') from item i where c.id = i.cart_id)
     )
    ) from cart c;
    
    • 演示
    create temporary table item (cart_id integer, price numeric, brand text);
    create temporary table cart (id integer, customer_id integer);
    insert into item values (1, 10, 'skoda'),(1, 11, 'ford'), (1, 12, 'opel'); 
    insert into item values (2, 20, 'skoda'),(2, 21, 'ford'), (2, 22, 'opel'); 
    insert into cart values (1, 1), (2, 2);
    
    select jsonb_agg(to_jsonb(c.*) || jsonb_build_object(
     'items', (select jsonb_agg(to_jsonb(i.*) - 'cart_id') from item i where c.id = i.cart_id)
    )) from cart c;
    
    • 结果:
    [
      {
        "id": 1,
        "items": [
          {
            "brand": "skoda",
            "price": 10
          },
          {
            "brand": "ford",
            "price": 11
          },
          {
            "brand": "opel",
            "price": 12
          }
        ],
        "customer_id": 1
      },
      {
        "id": 2,
        "items": [
          {
            "brand": "skoda",
            "price": 20
          },
          {
            "brand": "ford",
            "price": 21
          },
          {
            "brand": "opel",
            "price": 22
          }
        ],
        "customer_id": 2
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2021-03-19
      • 2017-07-08
      相关资源
      最近更新 更多