Can';在JSON值中找不到整个数组中的值

Can';在JSON值中找不到整个数组中的值,json,postgresql,postgresql-9.6,Json,Postgresql,Postgresql 9.6,Postgresql 9.6 json: 假设我要搜索第一个数组的项。我使用这个查询: SELECT product.data #>'{availability, 0, price}' from product 嗯。 但我需要在整个数组中查找(所有数组的项) 我需要像这样的smt(伪代码 SELECT product.data #>'{availability, *, price}' from product 可能吗 如果未找到price,则输出必须为空数组;如果在任意位置找到p

Postgresql 9.6

json:

假设我要搜索第一个数组的项。我使用这个查询:

SELECT product.data #>'{availability, 0, price}' from product
嗯。 但我需要在整个数组中查找(所有数组的项)

我需要像这样的smt(伪代码

SELECT product.data #>'{availability, *, price}' from product
可能吗

如果未找到price,则输出必须为空数组;如果在任意位置找到price,则输出必须为原始json

听起来像是一个奇怪的要求,但下面就可以做到这一点

select case 
           when (select jsonb_agg(e.item -> 'price') 
                 from jsonb_array_elements(data -> 'availability')  as e(item)
                 where e.item ? 'price') <> '[]' then data
           else '[]'::jsonb
       end
from product

这不是“全文搜索”。正如我之前所评论的。如果你想让事情变得更简单,你需要升级到Postgres 12,在那里你可以使用JSON/Path表达式来实现这一点。在9.6版本中,你将无法做到这一点。那么你想要的输出是什么?嵌入数组中包含所有价格值的数组?@a_horse_必须使用_no_name输出如果未找到price,则为空数组;如果在任何数组的(可用性)项中找到price,则为原始json。是否可以将结果返回为tsvector类型?
select case 
           when (select jsonb_agg(e.item -> 'price') 
                 from jsonb_array_elements(data -> 'availability')  as e(item)
                 where e.item ? 'price') <> '[]' then data
           else '[]'::jsonb
       end
from product
select (select jsonb_agg(e.item -> 'price') 
        from jsonb_array_elements(data -> 'availability')  as e(item)
        where e.item ? 'price') as prices
from product