简体   繁体   English

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

[英]postgres && - Array Overlap operator with wildcard

In postgres在 postgres

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

I'd like to know how I can use % wildcar in combination with && operator.我想知道如何将 % 通配符与 && 运算符结合使用。

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

I want to check if a text array contains at least one element of another text array.我想检查一个文本数组是否至少包含另一个文本数组的一个元素。 The first text array can contains wildcard.第一个文本数组可以包含通配符。 What's the best way to do that?最好的方法是什么?

You could try unnest to parse every element of both arrays and compare them using LIKE or ILIKE :您可以尝试unnest解析 arrays 的每个元素并使用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)

If you want to apply the % to all array elements , just place it directly in the WHERE clause in the LIKE or ILIKE operator:如果要将%应用于所有数组元素,只需将其直接放在LIKEILIKE运算符的WHERE子句中:

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)

Demo: db<>fiddle演示: db<>fiddle

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

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