简体   繁体   中英

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, ie, 1, 3 and 5 I want to run another select query:

select p_desc from public.products where p_id = 1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM