如何在Postgres中使用IN运算符绑定数组参数进行过滤

时间:2017-09-13 09:15:45

标签: sql arrays postgresql

我需要绑定包含值列表的参数,然后在查询中使用它来过滤UIColor.red.withAlphaComponent(0.5)运算符。假设我们有以下架构:

IN

基本上查询的行为如下:

create table test_table (
    id  serial primary key,
    channel text
);

insert into test_table(channel) values ('FOO'), ('BAR'), ('BAZ');

但我需要动态传递channels数组。我试过了:

select * from test_table
where channel in ('FOO', 'BAR');

所以我的问题是:如何对select * from test_table where channel in (string_to_array('FOO, BAR', ',')::text[]); ERROR: operator does not exist: text = text[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 42 运算符使用string_to_array? 或者如何将数组参数绑定到查询并将其用于IN

SQLFiddle

1 个答案:

答案 0 :(得分:3)

= any

where channel = any (string_to_array('FOO, BAR', ',')::text[]);

https://www.postgresql.org/docs/current/static/functions-comparisons.html#AEN21108