简体   繁体   English

在Postgres中的Array字段中搜索

[英]Search in Array field in Postgres

I have the following table structure here countries column is of array type. 我具有以下表结构,在这里countries列是数组类型。

 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: 要查找china or sriLanka所有行,请使用以下查询:

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? 就像In运算符一样,我们还能有更好的方法吗?

Maybe something like, SELECT * FROM country_list WHERE name in ('Asia','World'); 也许是这样, 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[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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