Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a select with array contains value clause in psql

I have column arr which is of type array.

I need to get rows, where arr column contains value s

This query:

SELECT * FROM table WHERE arr @> ARRAY['s'] 

gives the error:

ERROR: operator does not exist: character varying[] @> text[]

Why does it not work?

p.s. I know about any() operator, but why doesn't @> work?

like image 250
Oto Shavadze Avatar asked May 17 '13 10:05

Oto Shavadze


People also ask

Which option is used to check if array contains the value in PostgreSQL?

Check Value in Array Using UbiqUbiq Reporting tool supports all the above SQL queries and makes it easy to visualize SQL results in different ways.

How do I get an array query in SQL?

You need to use the ARRAY_AGG function to create an array that is the intermediate result of a SELECT statement, and then retrieve the contents of that array into an SQL array variable or parameter. For example: -- INTB IS AN OUT PARAMETER OF ORDINARY ARRAY TYPE INTARRAY. -- COL2 IS AN INTEGER COLUMN.


2 Answers

Try

SELECT * FROM table WHERE arr @> ARRAY['s']::varchar[] 
like image 116
Wojtas Avatar answered Sep 30 '22 04:09

Wojtas


Note that this may also work:

SELECT * FROM table WHERE s=ANY(array) 
like image 41
AetherUnbound Avatar answered Sep 30 '22 06:09

AetherUnbound