假设我的一个 postgres 列中有一个数组:
> select array[1,2,3,4];
array
-----------
{1,2,3,4}
通常如何从该数组中选择项目的子集?例如,如果我想从 x
索引到 y
索引中选择项目,我将如何获取该范围内的项目 (x
到 y
)?
我正在使用 PostgreSQL 9.4。
最佳答案
来自fine manual :
8.15.3. Accessing Arrays
[...]
We can also access arbitrary rectangular slices of an array, or subarrays. An array slice is denoted by writing lower-bound:upper-bound for one or more array dimensions.
[...]
It is possible to omit the lower-bound and/or upper-bound of a slice specifier; the missing bound is replaced by the lower or upper limit of the array's subscripts.
例如:
=> select (array[1,2,3,4,5,6])[2:5];
array
-----------
{2,3,4,5}
(1 row)
=> select (array[1,2,3,4,5,6])[:5];
array
-------------
{1,2,3,4,5}
(1 row)
=> select (array[1,2,3,4,5,6])[2:];
array
-------------
{2,3,4,5,6}
(1 row)
=> select (array[1,2,3,4,5,6])[:];
array
---------------
{1,2,3,4,5,6}
(1 row)
因此,要获取从索引 x
到 y
(含)的切片,您会说:
array_column[x:y]
关于arrays - 如何在 Postgres 中选择数组的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32793871/