Skip to content

Instantly share code, notes, and snippets.

@marcomalva
Created September 18, 2023 16:52
Show Gist options
  • Save marcomalva/1720b0fec0f24f13a4457914e35c28a9 to your computer and use it in GitHub Desktop.
Save marcomalva/1720b0fec0f24f13a4457914e35c28a9 to your computer and use it in GitHub Desktop.
[psql - search value in array ]Postgres: check if array field contains value? #psql
-- check if array contains a value or multiple values
-- answer copied from https://stackoverflow.com/a/54069718
-- search for single value in array
SELECT * FROM mytable WHERE 'Book' = ANY(pub_types);
-- search an array that contains multiple values **together**
SELECT * FROM mytable WHERE pub_types @> '{"Journal", "Book"}';
--search an array that contains **one of** some values.
SELECT * FROM mytable WHERE pub_types && '{"Journal", "Book"}';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment