postgres && - 带通配符的数组重叠运算符

postgres && - Array Overlap operator with wildcard

在 postgres 中

select array['some', 'word'] && array ['some','xxx']   -- return true
select array['some', 'word'] && array ['','word']   -- return true

我想知道如何将 % wildcar 与 && 运算符结合使用。

select array['some%', 'word'] && array ['','some']  -- I was thinking this will return true but it doesn't.

我想检查一个文本数组是否至少包含另一个文本数组的一个元素。第一个文本数组可以包含通配符。最好的方法是什么?

您可以尝试 unnest 解析两个数组的每个元素并使用 LIKEILIKE:

比较它们
SELECT EXISTS(
  SELECT  
  FROM unnest(array['some%', 'word']) i (txt),
       unnest(array ['','some']) j (txt)
  WHERE j.txt LIKE i.txt) AS overlaps;

 overlaps 
----------
 t
(1 row)

如果要将 % 应用于 所有数组元素 ,只需将其直接放在 LIKE 中的 WHERE 子句中即可或 ILIKE 运算符:

SELECT EXISTS(
  SELECT  
  FROM unnest(array['some', 'word']) i (txt),
       unnest(array ['','XXsomeXX']) j (txt)
  WHERE j.txt LIKE '%'||i.txt||'%') AS overlaps;

 overlaps 
----------
 t
(1 row)

演示:db<>fiddle