Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres SELECT where value in array

Tags:

postgresql

I have a postres table that looks like such: (shortened)

id (serial) | col1 (character varying[])
----------------------------------------
1           | {'Life', 'Health', "'VA Data'"}

I am trying to do the following:

SELECT * FROM mytable WHERE 'Life' = ANY (col1)

The results of this query, is zero records.

The goal being, I want any row, that has the value 'Life' within the col1 array.

What am I doing wrong?

like image 515
triunenature Avatar asked Oct 28 '25 10:10

triunenature


1 Answers

Any should work, but what you are showing me for output for the table is not Life but 'Life'. See the examples below (1. correctly inserted data and what it looks like; 2. incorrectly inserted data and what it looks like-looks like yours; 3. all data):

testdb=# select * from test where 'Life' = ANY(col1);
 id |          col1           
----+-------------------------
  1 | {Life,Health,"VA Data"}
(1 row)

testdb=# select * from test where '''Life''' = ANY(col1);
 id |           col1            
----+---------------------------
  2 | {'Life',Health,"VA Data"}
(1 row)

testdb=# select * from test;
 id |           col1            
----+---------------------------
  1 | {Life,Health,"VA Data"}
  2 | {'Life',Health,"VA Data"}
like image 77
Benjamin Bau Avatar answered Oct 30 '25 12:10

Benjamin Bau