带有文本数组的 sql IN 运算符

标签 sql postgresql

如何使用 SQL 查询在文本数组中查找字符串值。

假设我有:

id       location
1        {Moscow,New york}
2        {Mumbai}
3        {California,Texas}

我想找到位置是Moscow的id。我用过:

select id from table where location in ('Moscow'); 但出现错误:

ERROR:  malformed array literal: "Moscow"
LINE 1: select id from table where location in ('Moscow');
DETAIL:  Array value must start with "{" or dimension information.

我正在使用 Postgres。

最佳答案

对于DataType=Array,可以使用method=Any。

select id from table where 'Moscow' = Any(location)

作为文档which describes DataType=Array :

8.14.5. Searching in Arrays

To search for a value in an array, each value must be checked. This can be done manually, if you know the size of the array.

或使用 the method = Any :

9.21.3. ANY/SOME (array)

expression operator ANY (array expression) expression operator SOME (array expression) The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained. The result is "false" if no true result is found (including the case where the array has zero elements).

关于带有文本数组的 sql IN 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584808/

相关文章:

php - sql - 如何在 MySql 中截断多个表?

mysql - MySQL 中周期内的增量数

sql - 删除 SQL 中 created_at 的毫秒部分以与其他日期时间进行比较

postgresql - 为 postgres 灵活映射记录数组

sql - 在 SQL 中存储对象的动态属性

mysql - 对于所有现在或曾经在乐队中的艺术家来说,该艺术家在乐队中呆了多久

mysql - 自定义 AUTO INCREMENT 值不起作用

json - 如何使用 GROUP BY 子句将正确的属性名称设置为 json 聚合结果?

sql - 如何按周选择总活跃列表数?

database - Postgresql:遍历每个表并检查列?