Select any of several values ​​from the Postgres field

I have a table that resembles the following:

WORD WEIGHT WORDTYPE a 0.3 common the 0.3 common gray 1.2 colors steeple 2 object 

I need to immediately pull out the scales for several different words from the database. I could do:

 SELECT * FROM word_weight WHERE WORD = 'a' OR WORD = 'steeple' OR WORD='the'; 

but it seems ugly, and the code to generate the request is unpleasant. I hope I have a way to do something like (pseudocode):

 SELECT * FROM word_weight WHERE WORD = 'a','the'; 
+8
postgresql
source share
2 answers

You describe the functionality of the in clause.

select * from word_weight where word in ('a', 'steeple', 'the');

+14
source share

If you want to pass the entire list in one parameter, use the datatype array:

 SELECT * FROM word_weight WHERE word = ANY('{a,steeple,the}'); -- or ANY('{a,steeple,the}'::TEXT[]) to make explicit array conversion 
+8
source share

All Articles