0
我有一个表,如:PostgreSQL的查询JSON元素
CREATE TABLE stats
(
item character varying,
data jsonb
);
it contains values like
ITEM DATA
test1 [{"id":"1", "country":"UK"},{"id":"2", "country":"INDIA"}]
test2 [{"id":"1", "country":"US"},{"id":"4", "country":"CHINA"},{"id":"5", "country":"CHINA"}]
我需要不同的JSON对象的数量,其中国家是“中国”和产品“%测试%”; 在这种情况下,输出应该是2,即[{“id”:“4”,“country”:“CHINA”},{“id”:“5”,“country”:“CHINA”}]
我使用下面的查询
SELECT * FROM stats t
WHERE (data @> '[{"country":"CHINA"}]')
and t.item ilike '%test%';
output : [{"id":"1", "country":"US"},{"id":"4", "country":"CHINA"},{"id":"5", "country":"CHINA"}]
我应该做的,这样我得到的对象数组其中有“中国”作为国家?
ERROR:功能jsonb_agg(jsonb)不存在 SQL状态:42883 – ranjan
['jsonb_agg()'](https://www.postgresql.org/docs/9.5/static/functions-aggregate。 html)在当前的(9.5)Postgres版本中可用。在Postgres 9.4中,你可以试试['json_agg()'](https://www.postgresql.org/docs/9.4/static/functions-aggregate.html)。 – klin