我有一个表,其中有一列整数数组类型。 给定一个整数作为输入,我想查询该列名为的行 tipo_propiedad 包含输入整数。 直接在 Postgres 中可以通过使用 && 运算符并将输入 int 转换为数组来工作。 问题是使用这个:
Zone.where("tipo_propiedad && ?", [].push(type)).count
其中 type 是输入整数,给出
SELECT COUNT(*) FROM "zonas" WHERE (tipo_propiedad && 1) PG::UndefinedFunction: ERROR: operator does not exist: integer[] && integer
我也尝试过
Zone.where("tipo_propiedad && ?", [5].push(type))
因为 tipo_propiedad 里面只有 1、2 或 3,这就给出了
SELECT COUNT(*) FROM "zonas" WHERE (tipo_propiedad && 5,1) PG::UndefinedFunction: ERROR: operator does not exist: integer[] && integer
我也在使用 squeel 但该 gem 没有任何运算符来执行此操作。
最佳答案
你可以做任何一个
Zone.where("'?' = ANY (tipo_propiedad)", 1)
或
Zone.where("tipo_propiedad && ARRAY[?]", 1)
# or "&& '{?}'"
关于sql - 查询 int 是否在数组列内部,Postgres 和 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862674/