我有一张表格如下,
create table test
(
id varchar(20),
text varchar(20)
);
insert into test values(1, 'a')
insert into test values(2, 'b')
insert into test values(3, 'c')
我明白,要检查多列的where条件,我可以这样做
SELECT * FROM test WHERE (id, text) = ANY (VALUES('1', 'a'), ('2', 'b'), ('3', 'c'))
我有一个函数,需要 2 个数组作为输入,
create or replace function test_function
(
p_ids in varchar[],
p_texts in varchar[]
)
returns table (p_id varchar,
p_text varchar) as $$
begin
return query
SELECT id, text FROM test WHERE (id, text) = ANY (VALUES('1', 'a'), ('2', 'b'), ('3', 'c'))
end;
$$ LANGUAGE plpgsql;
;
我的问题是,将 ANY (VALUES('1', 'a'), ('2', 'b'), ('3', 'c')) 替换为实际的最有效方法是什么数组输入?
unnest()
构造函数中,在 row()
构造函数中 array()
它们:
create or replace function test_function
( p_ids in varchar[],
p_texts in varchar[]
)
returns table (p_id varchar,
p_text varchar) as $f$
begin
return query
SELECT id, text
FROM test
WHERE (id, text) = ANY(array(select (unnest(p_ids),unnest(p_texts))));
end;
$f$ LANGUAGE plpgsql;