Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select from any of multiple values from a Postgres field

Tags:

postgresql

I've got 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 pull the weights for several different words out of the database at once. I could do:

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

but it feels ugly and the code to generate the query is obnoxious. I'm hoping that there's a way I can do something like (pseudocode):

SELECT * FROM word_weight WHERE WORD = 'a','the';
like image 432
Winfield Trail Avatar asked Apr 24 '12 17:04

Winfield Trail


People also ask

How can I get multiple values from a single column in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);

How do I SELECT multiple columns in PostgreSQL?

If you specify a list of columns, you need to place a comma ( , ) between two columns to separate them. If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names.

What is := in PostgreSQL?

Equal (=) can be used instead of PL/SQL-compliant := = is for comparison. := is for assignment. Follow this answer to receive notifications.

How do I do a wildcard search in PostgreSQL?

Use ILIKE : SELECT * FROM table WHERE columnName ILIKE 'R%'; or a case-insensitive regular expression: SELECT * FROM table WHERE columnName ~* '^R.


1 Answers

You are describing the functionality of the in clause.

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

like image 86
Mark Wenzel Avatar answered Oct 15 '22 06:10

Mark Wenzel