postgresql - 范围日期的JSON查询

时间:2018-05-09 07:06:36

标签: php json postgresql

我有像这样的数据json

{"2018-05": /*year-month*/ {"20": /*date*/{"price": 50, "stock": 12}, "21":/*date*/{"price": 60, "stock": 5}, "25": /*date*/{"price": 55, "stock": 0}  }}

如何查询2日期和库存超过0的范围?

抱歉我的英语不好

1 个答案:

答案 0 :(得分:0)

我认为你正试图做像:

t=# with c(j) as (values('{"2018-05":{"20":{"price":50,"stock":12},"21":{"price":60,"stock":5},"25":{"price":55,"stock":0}}}'::json))
, s as (select j,json_object_keys(j->'2018-05') k from c)
select j->'2018-05'->k from s
where k::int between 20 and 22
and (j->'2018-05'->k->>'stock')::int > 0;
        ?column?
-------------------------
 {"price":50,"stock":12}
 {"price":60,"stock":5}
(2 rows)