我试图通过检查
uuids
是否与传递给 select 语句的数组中的任何值匹配来从表中获取行。
代码如下所示:
val existingCustomers = fetchRows(
ctx, generateSQLForTuples(tuples), mapOf("uuids" to customers.map { it["uuid"] as UUID })
)
generateSQLForTuples
看起来像这样:
private fun generateSQLForTuples(tuplesList: List<List<String>>):String =
// language=PostgreSQL
"""
select id, subcustomer
from customer
where uuid in (:uuids)
union
select id, subcustomer
from customer
where (customer_id, subcustomer) in (${toJdbcTuples(tuplesList)})
""".trimIndent()
但是,我收到错误:
PSQLException: ERROR: operator does not exist: uuid = uuid[]
我在这里做错了什么,如何将具有 UUID 值的数组传递给 select 语句?
IN
运算符不适用于 ARRAY[]
,它适用于一列行。
您有两种选择来解决此问题:
unnest()
例如。
column IN (SELECT unnest((ARRAY[uuid1])::uuid[]))
ANY()
与 ARRAY 一起使用例如。
WHERE column = ANY((ARRAY[uuid1])::uuid[])
我尝试使用 IN 但没有成功。我收到错误
ERROR: operator does not exist: uuid = uuid[]
。
我没有使用
WHERE SomeColumn IN (@ids)
,而是使用了 WHERE SomeColumn = ANY (@ids)
,并且它按预期工作。
您应该使用“=”而不是使用“IN”比较器,因为如果您使用的是 ARRAY[]