341

I'm sure this is a duplicate question in the sense that the answer is out there somewhere, but I haven't been able to find the answer after Googling for 10 minutes, so I'd appeal to the editors not to close it on the basis that it might well be useful for other people.

I'm using Postgres 9.5. This is my table:

        Column          │           Type            │                                Modifiers
────────────────────────┼───────────────────────────┼─────────────────────────────────────────────────────────────────────────
 id                     │ integer                   │ not null default nextval('mytable_id_seq'::regclass)
 pmid                   │ character varying(200)    │
 pub_types              │ character varying(2000)[] │ not null

I want to find all the rows with "Journal" in pub_types.

I've found the docs and googled and this is what I've tried:

select * from mytable where ("Journal") IN pub_types;
select * from mytable where "Journal" IN pub_types;
select * from mytable where pub_types=ANY("Journal");
select * from mytable where pub_types IN ("Journal");
select * from mytable where where pub_types contains "Journal";

I've scanned the postgres array docs but can't see a simple example of how to run a query, and StackOverflow questions all seem to be based around more complicated examples.

3
  • N.B. For PostgreSQL use single quotes for strings. Double quotes are for delimiter names (e.g. columns, indexes, etc.) Commented Mar 30, 2021 at 18:21
  • Does this answer your question? Check if value exists in Postgres array Commented Dec 1, 2021 at 10:14
  • There is a duplicate out there indeed. This is the accepted answer: stackoverflow.com/a/11231965/9266796. Be sure to check that one first because it contains important information regarding performance. Commented Dec 1, 2021 at 10:17

4 Answers 4

477

This should work:

select * from mytable where 'Journal'=ANY(pub_types);

i.e. the syntax is <value> = ANY ( <array> ). Also notice that string literals in postresql are written with single quotes.

Sign up to request clarification or add additional context in comments.

8 Comments

@redneb how about if I want to check if the Array field contains an item in an Array?
I'm getting ERROR: input of anonymous composite types is not implemented
Is that same as <value> IN ( <array> )?
IN expects an explicit list of values (or a subquery), whereas ANY works with arrays. This can be useful if you have the list of values already in an array, e.g. when the array is stored in some column in the db, as in the OP's case.
what about this syntax concerning performance? select * from mytable where pub_types @> array['Journal'::text];
|
298

With ANY operator you can search for only one value.

For example,

SELECT * FROM mytable WHERE 'Book' = ANY(pub_types);

If you want to search whether the array contains all of the values in another array, you can use the @> operator, aka the "contains" operator

"Does the first array contain the second".

For example,

SELECT * FROM mytable WHERE pub_types @> '{"Journal", "Book"}';

If you want to search whether the array contains any of the values in another array, you can use && operator.

"Do the arrays overlap, that is, have any elements in common"

For example,

SELECT * FROM mytable WHERE pub_types && '{"Journal", "Book"}';

You can specify in whichever order you like.

4 Comments

@> means contains all the values in that array. If you want to search if the current array contains any values in another array, you can use &&. select * from mytable where pub_types && '{"Journal", "Book"}';
I don't know if it's a version thing but both @> and && worked exactly the same for me on Postgres 9.6. They both matched any item in the list. Except that @> also matched a empty list '{}'.
Note that the @> '{"Journal", "Book"}' syntax does not seem to work in prepared statements (postgres did not recognize the $1 as a value placeholder, when provided with: @> '{$1, $2}'). So I used this alternate syntax instead (it's cleaner-looking anyway, imo): WHERE pub_types @> array['Journal', 'Book'];
What if pub_type is null? will it still works?
23

Using the Postgres array_to_string() method allowed me to match on 'Journal' appearing as part of a string in the array:

select * from mytable
where array_to_string(pub_types, ',') like '%Journal%'

If you only want to match the exact word 'Journal' remove the wildcards:

select * from mytable
where array_to_string(pub_types, ',') = 'Journal'

For reference: https://www.postgresql.org/docs/9.1/functions-array.html

7 Comments

This will yield false positives if you have multiple values with the same prefix, i.e. "Journal Entries"
The way the OP worded the question it seems like he wanted to find Journal appearing anywhere in the string. If you only want to match where it is specifically the word Journal just remove the leading and trailing wildcards characters (i.e. %).
Nice -- enabled me to do an ILIKE query over an array; thank you! SELECT * FROM archive WHERE ARRAY_TO_STRING(kw, ',') ILIKE '%pLASt%';
I don't think that's right about the B-tree comparison. According to the docs ( postgresql.org/docs/9.5/indexes-types.html )B-tree doesn't support @> only GIN does but I might be misreading.
@PeterGerdes: You are not misreading. There is misinformation in this answer.
|
8

Instead of IN we can use ANY with arrays casted to enum array, for example:

create type example_enum as enum (
  'ENUM1', 'ENUM2'
);

create table example_table (
  id integer,
  enum_field example_enum
);

select 
  * 
from 
  example_table t
where
  t.enum_field = any(array['ENUM1', 'ENUM2']::example_enum[]);

Or we can still use 'IN' clause, but first, we should 'unnest' it:

select 
  * 
from 
  example_table t
where
  t.enum_field in (select unnest(array['ENUM1', 'ENUM2']::example_enum[]));

Example: https://www.db-fiddle.com/f/LaUNi42HVuL2WufxQyEiC/0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.