如何在 WHERE IN 子句中使用 PostgreSQL 数组?

diz*_*iaq 4 arrays postgresql where-in

我正在尝试简化以下 SQL 语句(为了清晰起见,将其包装到函数中)。

使用array内部where ... in (/*array*/)子句的更简单/自然的语法方式是什么?(没有select * from unnest(...)样板)

CREATE OR REPLACE FUNCTION get_items(p_ids int[])
 RETURNS SETOF text
 LANGUAGE sql
AS $$
  select t.name 
    from my_table t 
   where f.id in (select * from unnest(p_ids))
$$;
 
 
 
 
Run Code Online (Sandbox Code Playgroud)

小智 7

不要使用INuse ANY,这也消除了取消嵌套的需要

where f.id = any (p_ids)
Run Code Online (Sandbox Code Playgroud)