4
How to filter this field of type array in Postgresql?
I have a table X
with a field Y
of the kind character varying[]
with two records:
{'meeting','lunch','training','presentation'}
{'breakfast','consulting', 'meeting'}
In this context how to make a query of the type:
SELECT * FROM X WHERE Y CONTAINS('lunch')
Try this: SELECT * FROM X WHERE 'lunch' = ANY (Y);
– cantoni
You didn’t call me back at all.
– user6406
Tried to
SELECT * FROM X WHERE Y @> ARRAY['lunch']::varchar[]
?– Maniero
Also not returned. The funny thing is that in the document itself does not show a query of this.
– user6406
It may be a problem in the data. Pity that the Sqlfiddle does not work right for me to test.
– Maniero
I tried Sqlfiddle tb and I couldn’t even create the schema. :-(
– cantoni
After many tests I realized something, cadas string was enclosed by a single quotation mark. {'Meeting','lunch','training','Presentation'} . The two solutions of @Cantoni and bigown are correct, the one of Cantoni with one limitation, but correct for the question.
– user6406
Get someone experienced in BD to help you. It’s totally possible to do this query.
– Felipe Jorge
These two solutions worked:
SELECT * FROM X WHERE 'lunch' = ANY (Y)
SELECT * FROM X WHERE Y @> ARRAY['lunch']::varchar[]
– user6406
@bigown, Cantoni, you can turn your comments into answers.
– user6406
Written @Andréhenriques. I received no notification of your comment, strange.
– cantoni