对基于数组的列中的每个值多次返回同一行

时间:2022-04-23 20:08:23

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?

...在不使用函数的单个查询中?

1 个解决方案

#1


4  

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

unexst()函数将执行您想要的操作

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

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

但是,返回元素的顺序是不确定的。

#1


4  

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

unexst()函数将执行您想要的操作

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

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

但是,返回元素的顺序是不确定的。