检查是否有任何值定数组是一个Postgres阵列present [英] Check if any of a given array of values are present in a Postgres array

查看:154
本文介绍了检查是否有任何值定数组是一个Postgres阵列present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Rails应用程序4和名为标识符我的车型之一有一个字符串数组列。当用户通过在标识符列表,什么是抓取,其中任何给定的标识符相匹配的所存储的标识符的行的语法

I'm writing a Rails 4 app and have a string array column on one of my models called identifiers. When the user passes in a list of identifiers, what's the syntax to fetch the rows where any of the given identifiers match any of the stored identifiers?

我试图在哪里('ARRAY [?] ==任何(标识符),IDS),但似乎并没有工作。

I'm trying where('ARRAY[?] == any(identifiers)', ids), but that doesn't seem to work.

推荐答案

使用的数组运算符重叠: &放大器;&安培;

SELECT * FROM tbl WHERE ARRAY[1,2,3] && identifiers

或者

SELECT * FROM tbl WHERE '{1,2,3}'::int[] && identifiers

您没有透露确切的类型,当然必须匹配。我的例子是整型数组。

You did not disclose the exact type, which must match, of course. My example is for integer arrays.

这种形式可以利用的标识列GIN索引:

This form can utilize a GIN index on the identifiers column:

CREATE INDEX tbl_identifiers_gin_idx ON tbl USING GIN (identifiers);

这篇关于检查是否有任何值定数组是一个Postgres阵列present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆