简体   繁体   中英

PostgreSQL How to query String array


I am trying to write a query to check if an element is within an array of Strings.

Here is my simple select query along with the output

select languages from person limit 3;
{CSS,HTML,Java,JavaScript,Python}
{JavaScript,Python,TensorFlow}
{C++,Python}

How do I write a query to find all people who have "Java" as a listed language they know?
I tried following the syntax but it isn't working.

select languages from person where languages @> ARRAY['Java']::varchar[];

尝试这个

select languages from person where 'Java' = ANY (string_to_array(languages , ','))

You need to use a string constant on the left side, and the ANY operator on the array column:

select languages 
from person 
where 'Java' = any(languages);

This assumes languages is defined as text[] or varchar[] as your sample output indicates

您可以搜索多个模式,用 POSIX 正则表达式前面的正则表达式匹配运算符 '~' 替换 '=' 运算符,例如:

select languages from person where '[Java,Php]' ~ ANY (string_to_array(languages , ','))

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