Skip to content
Advertisement

how to write select query in PostgreSQL to iterate over an array which is returned by a select query

SELECT b_items_p_id FROM  public.box WHERE b_id =1

and this is what it returns:

{1,3,5}

Now on each of these value, i.e., 1, 3 and 5 I want to run another select query:

select p_desc from public.products where p_id = 1

Advertisement

Answer

You can join box and products using the any() operator, like so:

select p_desc 
from public.box b 
inner join public.products p on p.p_id = any(b.b_items_p_id)
where b.b_id =1

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement