![](/img/trans.png)
[英]Postgres jsonb column select from array where a string matches wildcards
[英]Postgres get all elements where value matches from jsonb array
我想我正在尝试一些简单的事情,但几个小时后我就是无法让它工作。 我有一个包含标签列的表,它是一个 jsonb 数组,如下所示:
{"{\"name\": \"e-commerce\", \"weight\": 400}","{\"name\": \"management\", \"weight\": 200}","{\"name\": \"content management\", \"weight\": 200}"}
我现在想编写一个查询,当名称与搜索字符串匹配时,它会向我返回完整的 object。 到目前为止,我想出了这个:
SELECT * FROM data
WHERE tags is not null
AND EXISTS(
SELECT FROM data ad WHERE (ad.tags -> 'name' ->> 'education')::text
);
但我得到这个错误:
[42883] ERROR: operator does not exist: jsonb[] -> unknown Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
我不确定我应该在哪里进行类型转换,以及这是否真的是问题所在。
如果您希望每个匹配的 object 在单独的行上,您可以使用jsonb_array_elements()
对象数组,然后过滤:
select o.obj
from data d
cross join lateral jsonb_array_elements(d.tags) o(obj)
where o.obj ->> 'name' = 'education'
这适用于你有 JSONB 数组(所以数据的数据类型是jsonb
)。
另一方面,如果您有一个 json 对象数组(例如: unnest
jsonb[]
),则可以取消嵌套:
select o.obj
from data d
cross join lateral unnest(d.tags) o(obj)
where o.obj ->> 'name' = 'education'
请注意,当两个对象在同一个数组中匹配时,这会生成两行。 如果你只想要一行,你可以使用exists
代替:
select o.obj
from data d
where exists (
select 1 from unnest(d.tags) o(obj) where o.obj ->> 'name' = 'education'
)
您需要查询json array
中的对象。 使用jsonb_array_elements
创建这些对象,然后像查询 json 一样 -
SELECT d.* FROM data d, jsonb_array_elements(d.tags) tag
WHERE tag is not null and WHERE (tag -> 'name') ? 'education'
另外,请注意,在您的原始查询中
这个 -
WHERE (ad.tags -> 'name' ->> 'education')::text
应该 -
WHERE (ad.tags -> 'name') ? 'education'
或者
WHERE (ad.tags ->> 'name')::text = 'education'
由于您的数据类型不是 jsonb 而是 jsonb[],因此您需要使用unnest
将其取消嵌套到 jsonb -
SELECT * FROM data d, jsonb_array_elements(unnest(d.tags)) tagObj
WHERE tagObj is not null and WHERE (tag -> 'name') ? 'education'
或者
select * from (
SELECT * FROM data d, jsonb_array_elements(unnest(d.tags)) tagobj
) a WHERE tag is not null and WHERE (tag -> 'name') ? 'education'
第一个可能会出错,因为tagObj
在该上下文中不可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.