Har*_*jan 2 sql database postgresql where any
我试图使用PostgreSQL的ANY函数从数组interger类型列中搜索值.
我的SQL:
SELECT
*
FROM
company_employee_contacts
WHERE
corporate_complaint_type_ids = ANY(ARRAY[1,3]::integer[])
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误:
错误:运算符不存在:整数[] =整数
任何人都可以告诉我为什么我在进行类型转换时会收到此错误?
因为corporate_complaint_type_ids不是整数,而是整数数组...你不能:
select '{2,3,4}'::int[] = ANY(ARRAY[1,3]::integer[]);
ERROR: operator does not exist: integer[] = integer
LINE 1: select '{2,3,4}'::int[] = ANY(ARRAY[1,3]::integer[]);
Run Code Online (Sandbox Code Playgroud)
相反,您可以检查数组是否重叠:
postgres@pond93# select '{2,3,4}'::int[] && ARRAY[1,3]::integer[];
?column?
----------
t
(1 row)
Run Code Online (Sandbox Code Playgroud)
或者您可以针对ANY(数组)检查一个数组值:
postgres@pond93# select ('{2,3,4}'::int[])[1] = ANY(ARRAY[1,3]::integer[]);
?column?
----------
f
(1 row)
Run Code Online (Sandbox Code Playgroud)