【问题标题】:Postgresql join on jsonbPostgresql 加入 jsonb
【发布时间】:2015-06-01 02:07:53
【问题描述】:

我有桌子。

订单表:

id: integer
items: jsonb

物品表:

id: integer
title: string
price: integer

示例订单记录

id: 1, 
items: {
   "1"=>{"qty"=>3},
   "3"=>{"qty"=>12}
    }

示例项目记录

 id:1, title: "Tesla model S"
 id:2, title: "Tesla model W"
 id:3, title: "Tesla model D"

请帮助获得结果:

id:1, title: "Tesla model S", qty: 3
id:3, title: "Tesla model D", qty: 12

【问题讨论】:

    标签: postgresql join


    【解决方案1】:

    最后,我做到了。 该查询有效。

            SELECT
            json_data.key AS id, 
            json_data.value::jsonb->'qty' AS qty,
            items.title as title 
            FROM orders, jsonb_each_text(orders.items) AS json_data
            INNER JOIN items ON items.id = json_data.key::int
            WHERE orders.id=1
    

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2014-05-04
      • 2023-01-04
      • 1970-01-01
      • 2015-02-18
      • 2021-10-19
      相关资源
      最近更新 更多