Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: Find matching array or array that contains t

I'm using postgres 9.5 and have a table that looks something like this:

+----+--------------+-------------------------------+
| id | string_field | array_field                   |
|----+--------------+-------------------------------|
| 1  | string a     | [apple, orange, banana, pear] |
| 2  | string b     | [apple, orange, banana]       |
| 3  | string c     | [apple, orange]               |
| 4  | string d     | [apple, pear]                 |
| 5  | string e     | [orange, apple]               |
+----+--------------+-------------------------------+

Is it possible to query the DB for rows where array_field is, or contains [apple, orange, banana]? The results should return rows with id 1 and 2.

like image 834
Zaki Aziz Avatar asked Dec 01 '25 04:12

Zaki Aziz


1 Answers

Try something like this:

where array_field @> ARRAY['apple', 'orange', 'banana']::varchar[]

Documentation

like image 92
potashin Avatar answered Dec 02 '25 18:12

potashin