HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Where column not like multiple values

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnwherelikemultiplevaluesnot

Problem

I am trying to select records in a postgresql db where username is not like a list of strings.

SELECT * FROM rails_db WHERE username NOT LIKE 'j%' AND username NOT LIKE '%eepy%';


The issue is there are a lot of these values. Is there a way to create an array of those and say something like:

SELECT * FROM rails_db WHERE username NOT LIKE ARRAY[my values];

Solution

You almost had the correct syntax. This is exactly what you want:

SELECT * FROM rails_db WHERE username NOT LIKE ALL(ARRAY[my values]);


This is a very nice syntax, but it won't necessarily be fast, especially not if the array is large. However, what you want is hard to optimize for performance, and there is no reason to think uglier syntaxes will be any faster.

Code Snippets

SELECT * FROM rails_db WHERE username NOT LIKE ALL(ARRAY[my values]);

Context

StackExchange Database Administrators Q#121357, answer score: 23

Revisions (0)

No revisions yet.