Skip to content
Advertisement

How to select from a column with a list of ids in postgresql

I’ve got mytable1 with row_number integer and list_of_ids int[] column

mytable2 with id integer and company text columns

Example entry for mytable1

Example entry for mytable2

I need to feed back values from mytable2 into mytable1. This way the expected output would be

Advertisement

Answer

You need to unnest the lists of ids, join mytable2 using unnested ids and finally aggregate back the data to get a single row for a row_number.

Db<>fiddle.

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