【发布时间】:2016-11-25 11:53:37
【问题描述】:
这是我的订单表的样子:
-----------------------------------------------------------
| id | order
-----------------------------------------------------------
|1 |[{"order_quantity" : 2, "active" : TRUE, "price" : $100 }, {"order_quantity" : 4, "active" : FALSE, "price" : $200 }]
|2 |[{"order_quantity" : 2, "active" : TRUE, "price" : $170 }]
|3 |[{"order_quantity" : 2, "active" : TRUE, "price" : $120 }]
|4 |[{"order_quantity" : 2, "active" : TRUE, "price" : $150 }, {"order_quantity" : 3, "active" : TRUE, "price" : $200 }, {"order_quantity" : 5, "active" : TRUE, "price" : $200 }]
-----------------------------------------------------------
计算每个元素中括号 WHERE active == TRUE 内的 JSON 元素时想要的结果:
------------
id | counts
------------
|1 | 1
|2 | 1
|3 | 1
|4 | 3
------------
这是我正在使用的,但它没有提供我正在寻找的数据,因为它不会查看每个字典以查看 active == TRUE
SELECT id, json_array_length(order::JSON)
FROM orders
------------
id | counts
------------
|1 | 2
|2 | 1
|3 | 1
|4 | 3
------------
【问题讨论】:
-
select id, array_length(string_to_array(orders::text, 'TRUE'), 1) - 1 as counts from ...
标签: json postgresql arraylist multiple-columns