如何使用 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/