These two queries are giving different results in postgresql
not (field_name like any(array['a','b'])
is giving values that are not a or b as expected- whereas
field_name not like any(array['a','b'])
is checking for both a and b values
I basically want to fetch records not having either a or b. I’ve gone through postgres documentation did not find any result
2
Answers
Im not sure what you’re asking for since you already know the difference between the two queries already. The first query is checking for values that are not equal to ‘a’ or ‘b’ which is what you want.
The second query is checking if ‘field_name’ has any occurrence of the patterns ‘a’ or ‘b’ it will not return the record. In other words, it will return only the records where there are no matching patterns i.e no ‘a’ or ‘b’.
The 2nd query makes no sense:
field_name not like any(array['a','b'])
is the same asfield_name not like 'a' OR field_name not like 'b'
, so except fornull
, it will always betrue
(fieldname
can’t bea
andb
at the same time).If you want to rewrite the 1st query, you would have to use
ALL
field_name not like ALL(array['a','b'])
, which is the same asfield_name not like 'a' AND field_name not like 'b'