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.

A J
  • 1,439
  • 4
  • 25
  • 42

1 Answers1

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

Jay Ehsaniara
  • 1,421
  • 17
  • 24
  • 1
    Hope your code may help. I was wrong about column type. I have added a demo, please & verify that. – Sinto Sep 13 '18 at 05:22