postgresql 如何在psql中使用包含值子句的数组进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16606357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:10:48  来源:igfitidea点击:

How to make a select with array contains value clause in psql

postgresqlpostgresql-9.2

提问by Oto Shavadze

I have column arrwhich is of type array.

我有arr类型的列array

I need to get rows, where arrcolumn contains value s

我需要获取行,其中arr列包含值s

This query:

这个查询:

SELECT * FROM table WHERE arr @> ARRAY['s']

gives the error:

给出错误:

ERROR: operator does not exist: character varying[] @> text[]

错误:运算符不存在:字符变化[] @> text[]

Why does it not work?

为什么它不起作用?

p.s. I know about any()operator, but why doesn't @>work?

ps 我知道any()运营商,但为什么不@>工作?

回答by Wojtas

Try

尝试

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

回答by AetherUnbound

Note that this may also work:

请注意,这也可能有效:

SELECT * FROM table WHERE s=ANY(array)

回答by vol7ron

SELECT * FROM table WHERE arr && '{s}'::text[];

Compare two arrays for containment.

比较两个数组的包含情况。