Sql 如何在Postgres中绑定数组参数以使用IN运算符进行过滤
我需要绑定包含值列表的参数,然后在查询中使用中的运算符进行过滤。假设我们有以下模式:Sql 如何在Postgres中绑定数组参数以使用IN运算符进行过滤,sql,arrays,postgresql,Sql,Arrays,Postgresql,我需要绑定包含值列表的参数,然后在查询中使用中的运算符进行过滤。假设我们有以下模式: create table test_table ( id serial primary key, channel text ); insert into test_table(channel) values ('FOO'), ('BAR'), ('BAZ'); 基本上,查询的行为类似于: select * from test_table where channel in ('FOO', '
create table test_table (
id serial primary key,
channel text
);
insert into test_table(channel) values ('FOO'), ('BAR'), ('BAZ');
基本上,查询的行为类似于:
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
所以我的问题是:如何在操作符中为使用字符串到数组?
或者如何将数组参数绑定到查询并将其用于
中的
=任何
:
where channel = any (string_to_array('FOO, BAR', ',')::text[]);