简体   繁体   中英

postgres && - Array Overlap operator with wildcard

In 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 :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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