我正在使用postgres 9.4和jsonb类型。我有下表:
CREATE TABLE jsonb_test (iid serial NOT NULL, data jsonb)
以及该表中的以下json数据:
{"date": "2016-01-01T00:00:00.000Z", "items": [{"name": "bottles", "price": 12}, {"name": "caps", "price": 20}], "customer": {"name": "Customer 1", "email": "[email protected]"}}
{"date": "2015-12-01T00:00:00.000Z", "items": [{"name": "bottles", "price": 1}, {"name": "caps", "price": 50}], "customer": {"name": "Customer 2", "email": "[email protected]"}}
我想做的是获取所有包含价格大于30的商品的行。在这种情况下,该行将返回Customer 2行。
到目前为止,这是我的查询:
SELECT * FROM jsonb_test WHERE jsonb_array_elements(jsonb_extract_path(data, 'items')) #> '{price}' > '0'
但这失败并显示错误 ERROR: argument of WHERE must not return a set
谁能告诉我该怎么做?是否可以针对任意级别的嵌套执行此操作?
谢谢!
尝试这样的事情:
SELECT * FROM jsonb_test, jsonb_array_elements(jsonb_test.data->'items') AS a WHERE (a->>'price') > '0'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句