Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PostgreSQL array in WHERE IN clause?

I am trying to simplify the following SQL statement (wrapped into function for clarity).

What is a simpler/natural syntactical way to use array inside where ... in (/*array*/) clause? (without select * from unnest(...) boilerplate)

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))
$$;
 
 
 
 
like image 872
diziaq Avatar asked Nov 04 '25 16:11

diziaq


1 Answers

Don't use IN use ANY, this also removes the need to unnest

where f.id = any (p_ids)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!