Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return same row multiple times for each value in an array-based column

Say I have a table with an array column:

id | subIds
1  | {1,2,3}
2  | {4,5}

How would I return the resultset:

id | subId    
1  | 1
1  | 2
1  | 3
2  | 4
2  | 5

... in a single query without using a function?

like image 970
SpliFF Avatar asked Aug 02 '26 03:08

SpliFF


1 Answers

By "without using a function" I assume you mean "without writing my own function to do it".

The unnest() function will do what you want

select id, unnest(subids) as subid
from the_table;

The order on how the elements are returned is undefined though.


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!