1

I have the following table structure here countries column is of array type.

 id | name  |             countries             
----+-------+-----------------------------------
  1 | Asia  | {india,china,pakistan,bangladesh}
  2 | World | {india,pakistan,srilanka}

To find all rows of china or sriLanka I use below query:

SELECT * 
FROM country_list 
WHERE 'china' = ANY(country_list.countries) 
   OR 'srilanka' = ANY(country_list.countries);

Can we have any better way to do this just like In operator?

Maybe something like, SELECT * FROM country_list WHERE name in ('Asia','World');?

2 Answers 2

4

You can use the && overlaps operator:

select *
from country_list
where countries && array['china','srilanka'];
Sign up to request clarification or add additional context in comments.

Comments

0

Try This Query Like.

SELECT * FROM table WHERE name @> ARRAY['s']::varchar[]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.