moonshot
moonshot

Reputation: 699

Search Array of Strings in Postgres

I'm using Postgres 9.5 and I have a column col with arrays like this

["ABC","4"]
["4","5","6"]
["ABC"]

How do I search for all arrays that contain 'ABC'?

I've tried everything - @>, ANY, etc and looked at 20 questions on SO. This is the closest I got but it only returns exact matches.

select * from t 
where col = '["ABC"]';

Upvotes: 0

Views: 154

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246113

You should construct a single-element array and use the “contains” operator:

SELECT * FROM t
WHERE arr @> ARRAY['ABC'];

A GIN index can speed up this search.

Upvotes: 1

Related Questions