1

I have a table with a column 'sample_column' which is an array.

Can anyone tell me how can I select data based on 'sample_column' in postgresql?

Example of data in sample_column: ["one","two","three"]

I want to get all data if sample_column has value "three"

Here is what I have done:

Select * from sample_table where sample_column contains 'three'

I am getting ERROR.

Any help will be appreciated.

4
  • What was the error? You can try SELECT * FROM sample_table WHERE sample_column LIKE '%three%' Commented Sep 13, 2018 at 4:54
  • "syntax error at or near contains" Commented Sep 13, 2018 at 4:56
  • @Sinto now getting "operator does not exist" Commented Sep 13, 2018 at 4:58
  • Can you add a sample screenshot of column with data Commented Sep 13, 2018 at 5:01

1 Answer 1

3

I assume you have table as:

CREATE TABLE table_name
(
    sample_column text[]
);

and you have insert data as:

insert into table_name(sample_column) values (array['one','two','three']);
insert into table_name(sample_column) values (array['yes','no']);
insert into table_name(sample_column) values (array['red','white','blue']);

now you want to find recored based on the array element:

select * from table_name where 'three' = ANY(sample_column);

I hope it helps.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Hope your code may help. I was wrong about column type. I have added a demo, please & verify that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.