我有桌子。
订单表:
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
答案 0 :(得分:0)
最后,我做到了。 该查询有效。
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