简体   繁体   中英

Search in Array field in Postgres

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'); ?

You can use the && overlaps operator :

select *
from country_list
where countries && array['china','srilanka'];

尝试类似此查询。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM