8.15.3. Accessing Arrays
8.15.3.访问数组
Now, we can run some queries on the table. First, we show how to access a single element of an array.This query retrieves the names of the employees whose pay changed in the second quarter:
现在,让我们针对表执行几个查询。首先,展示一下如何访问数组的单个元素。以下查询检索在第二季度工资有变化的员工:
SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
name
-------
Carol
(1 row)
The array subscript numbers are written within square brackets. By default PostgreSQL uses a onebased numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].
数组下标数字写在方括号内。默认情况下,PostgreSQL对数组使用基于1的编号约定,即,n个元素的数组以array [1]开头,以array [n]结尾。
This query retrieves the third quarter pay of all employees:
以下查询检索所有员工的第三季度工资:
SELECT pay_by_quarter[3] FROM sal_emp;
pay_by_quarter
----------------
10000
25000
(2 rows)
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. For example, this query retrieves the first item on Bill's schedule for the first two days of the week:
我们还可以访问数组或子数组的任意矩形切片。 数组切片通过为一个或多个数组维编写lower-bound:upper-bound来表示。 例如,此查询在一周的前两天检索比尔的日程安排中的第一项:
SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{meeting},{training}}
(1 row)
If any dimension is written as a slice, i.e., contains a colon, then all dimensions are treated as slices. Any dimension that has only a single number (no colon) is treated as being from 1 to the number specified. For example, [2] is treated as [1:2], as in this example:
如果将任何维度写为切片,即包含冒号,则所有维度都将视为切片。 任何只有一个数字(无冒号)的维将被视为从1到指定的数字。 例如,在此示例中,将[2]视为[1:2]:
SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
schedule
-------------------------------------------
{{meeting,lunch},{training,presentation}}
(1 row)
To avoid confusion with the non-slice case, it's best to use slice syntax for all dimensions, e.g., [1:2] [1:1], not [2][1:1].
为避免与非切片情况混淆,最好对所有维度使用切片语法,例如[1:2] [1:1],而不是[2] [1:1]。
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. For example:
可以省略切片说明符的下限和/或上限。 缺少的边界将替换为数组下标的下限或上限。 例如:
SELECT schedule[:2][2:] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{lunch},{presentation}}
(1 row)
SELECT schedule[:][1:1] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{meeting},{training}}
(1 row)
An array subscript expression will return null if either the array itself or any of the subscript expressions are null. Also, null is returned if a subscript is outside the array bounds (this case does not raise an error). For example, if schedule currently has the dimensions [1:3][1:2] then referencing schedule[3][3] yields NULL. Similarly, an array reference with the wrong number of subscripts yields a null rather than an error.
如果数组本身或任何下标表达式为null,则数组下标表达式将返回null。另外,如果下标超出数组范围,则返回null(这种情况不会引发错误)。例如,如果schedule当前具有维度[1:3] [1:2],则引用schedule [3] [3]会产生NULL。同样,下标数目错误的数组引用会产生null而不是错误。
An array slice expression likewise yields null if the array itself or any of the subscript expressions are null. However, in other cases such as selecting an array slice that is completely outside the current array bounds, a slice expression yields an empty (zero-dimensional) array instead of null. (This does not match non-slice behavior and is done for historical reasons.) If the requested slice partially overlaps the array bounds, then it is silently reduced to just the overlapping region instead of returning null.
如果数组本身或任何下标表达式为null,则数组切片表达式同样会产生null。 但是,在其他情况下,例如选择完全超出当前数组范围的数组切片,切片表达式会产生一个空(零维)数组,而不是null。 (这与非切片行为不匹配,是由于历史原因。)如果请求的切片部分与数组边界重叠,则将其静默地缩小为重叠区域,而不是返回null。
The current dimensions of any array value can be retrieved with the array_dims function:
可以使用array_dims函数检索任何数组值的当前维:
SELECT array_dims(schedule) FROM sal_emp WHERE name = 'Carol';
array_dims
------------
[1:2][1:2]
(1 row)
array_dims produces a text result, which is convenient for people to read but perhaps inconvenient for programs. Dimensions can also be retrieved with array_upper and array_lower,which return the upper and lower bound of a specified array dimension, respectively:
array_dims会生成一个文本结果,这对于人们来说很方便阅读,但对于程序来说可能不方便。 也可以使用array_upper和array_lower检索维度,它们分别返回指定数组维度的上限和下限:
SELECT array_upper(schedule, 1) FROM sal_emp WHERE name = 'Carol';
array_upper
-------------
2
(1 row)
array_length will return the length of a specified array dimension:
array_length返回数组长度:
SELECT array_length(schedule, 1) FROM sal_emp WHERE name = 'Carol';
array_length
--------------
2
(1 row)
cardinality returns the total number of elements in an array across all dimensions. It is effectively the number of rows a call to unnest would yield:
cardinality返回所有维度上数组中元素的总数。 实际上,即调用unnest会产生的行数:
SELECT cardinality(schedule) FROM sal_emp WHERE name = 'Carol';
cardinality
-------------
4
(1 row)