简体   繁体   English

PostgreSQL json数组交集查询

[英]PostgreSQL json array intersection query

I have a table with a jsonb data column, which looks like this: 我有一个带有jsonb 数据列的表,看起来像这样:

data: {
    "categories": [
        "Category A",
        "Category D"
    ],
    "something": "dsa",
}

I would like to query rows that have one or more strings existing inside categories array (not empty intersection). 我想查询类别数组(不是空交集)中存在一个或多个字符串的行。

Let's suppose these strings to check against are "Category A" and "Category B". 让我们假设要检查的这些字符串是“ Category A”和“ Category B”。

How would such query look like? 这样的查询看起来如何?

Here's a query that does similar thing except it checks for all supplied strings to be existing in categories array: 这是一个执行类似操作的查询,但它会检查所有提供的字符串是否存在于category数组中:

SELECT *
FROM table
WHERE data->'categories' @> '["Category A", "Category B"]'

I need this query to match at least one string, not all. 我需要此查询来匹配至少一个字符串,而不是全部。

There is a ?| 有一个?| operator taking jsonb and text[] described as exists any : 采用jsonbtext[] 运算符描述为exists any

select '["Category A", "Category D"]'::jsonb ?| array['Category A', 'Category B'];
 ?column? 
----------
 t

select '["Category A", "Category D"]'::jsonb ?| array['Category Ax', 'Category B'];
 ?column? 
----------
 f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM