Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to check to see if an array contains another array in postgres?

I am trying to write an postgresql query and one of the where statements is a check to see if an array of filters that a user passes contains a value the array of types on a database column has in a json blob.
for example, if the user sets their filters to look for values
["prettyfly","likespizza"]

and the table has

["eatingfood","prettyfly"]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

in a column it would return that row. Here is what I’ve tried, but it isn’t quite right

filterQuery = `AND ['${coreValueFilters.join("','")}'] = ANY(hashtags)`

hashtags being the name of the table.

Here is a clearer example without variables

filterQuery = `AND ["prettyfly","likespizza"] = ANY(hashtags)`

>Solution :

Use &&

anyarray && anyarray → boolean
Do the arrays overlap, that is, have any elements in common?
ARRAY[1,4,3] && ARRAY[2,1] → t

From https://www.postgresql.org/docs/current/functions-array.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading