检查是否有任何值定数组是一个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);