数组函数和操作符
数组操作符
- =
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3] AS RESULT ; result -------- t (1 row)
- <>
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,2,3] <> ARRAY[1,2,4] AS RESULT; result -------- t (1 row)
- <
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,2,3] < ARRAY[1,2,4] AS RESULT; result -------- t (1 row)
- >
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,4,3] > ARRAY[1,2,4] AS RESULT; result -------- t (1 row)
- <=
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,2,3] <= ARRAY[1,2,3] AS RESULT; result -------- t (1 row)
- >=
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,4,3] >= ARRAY[1,4,3] AS RESULT; result -------- t (1 row)
- @>
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,4,3] @> ARRAY[3,1] AS RESULT; result -------- t (1 row)
- <@
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[2,7] <@ ARRAY[1,7,4,2,6] AS RESULT; result -------- t (1 row)
- &&
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,4,3] && ARRAY[2,1] AS RESULT; result -------- t (1 row)
- ||
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[1,2,3] || ARRAY[4,5,6] AS RESULT; result --------------- {1,2,3,4,5,6} (1 row)
1 2 3 4 5
openGauss=# SELECT ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]] AS RESULT; result --------------------------- {{1,2,3},{4,5,6},{7,8,9}} (1 row)
- ||
示例:
1 2 3 4 5
openGauss=# SELECT 3 || ARRAY[4,5,6] AS RESULT; result ----------- {3,4,5,6} (1 row)
- ||
示例:
1 2 3 4 5
openGauss=# SELECT ARRAY[4,5,6] || 7 AS RESULT; result ----------- {4,5,6,7} (1 row)
数组比较是使用默认的B-tree比较函数对所有元素逐一进行比较的。多维数组的元素按照行顺序进行访问。如果两个数组的内容相同但维数不等,决定排序顺序的首要因素是维数。
数组函数
- array_append(anyarray, anyelement)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_append(ARRAY[1,2], 3) AS RESULT; result --------- {1,2,3} (1 row)
- array_prepend(anyelement, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_prepend(1, ARRAY[2,3]) AS RESULT; result --------- {1,2,3} (1 row)
- array_cat(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5 6 7 8 9 10 11
openGauss=# SELECT array_cat(ARRAY[1,2,3], ARRAY[4,5]) AS RESULT; result ------------- {1,2,3,4,5} (1 row) openGauss=# SELECT array_cat(ARRAY[[1,2],[4,5]], ARRAY[6,7]) AS RESULT; result --------------------- {{1,2},{4,5},{6,7}} (1 row)
- array_union(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_union(ARRAY[1,2,3], ARRAY[3,4,5]) AS RESULT; result ------------- {1,2,3,3,4,5} (1 row)
- array_union_distinct(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_union_distinct(ARRAY[1,2,3], ARRAY[3,4,5]) AS RESULT; result ------------- {1,2,3,4,5} (1 row)
- array_intersect(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_intersect(ARRAY[1,2,3], ARRAY[3,4,5]) AS RESULT; result ------------- {3} (1 row)
- array_intersect_distinct(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_intersect_distinct(ARRAY[1,2,2], ARRAY[2,2,4,5]) AS RESULT; result ------------- {2} (1 row)
- array_except(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_except(ARRAY[1,2,3], ARRAY[3,4,5]) AS RESULT; result ------------- {1,2} (1 row)
- array_except_distinct(anyarray, anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_except_distinct(ARRAY[1,2,2,3], ARRAY[3,4,5]) AS RESULT; result ------------- {1,2} (1 row)
- array_ndims(anyarray)
返回类型:int
示例:
1 2 3 4 5
openGauss=# SELECT array_ndims(ARRAY[[1,2,3], [4,5,6]]) AS RESULT; result -------- 2 (1 row)
- array_dims(anyarray)
返回类型:text
示例:
1 2 3 4 5
openGauss=# SELECT array_dims(ARRAY[[1,2,3], [4,5,6]]) AS RESULT; result ------------ [1:2][1:3] (1 row)
- array_length(anyarray, int)
返回类型:int
示例:
1 2 3 4 5
openGauss=# SELECT array_length(array[1,2,3], 1) AS RESULT; result -------- 3 (1 row)
- array_lower(anyarray, int)
返回类型:int
示例:
1 2 3 4 5
openGauss=# SELECT array_lower('[0:2]={1,2,3}'::int[], 1) AS RESULT; result -------- 0 (1 row)
- array_sort(anyarray)
返回类型:anyarray
示例:
1 2 3 4 5
openGauss=# SELECT array_sort(ARRAY[5,1,3,6,2,7]) AS RESULT; result ------------- {1,2,3,5,6,7} (1 row)
- array_upper(anyarray, int)
返回类型:int
示例:
1 2 3 4 5
openGauss=# SELECT array_upper(ARRAY[1,8,3,7], 1) AS RESULT; result -------- 4 (1 row)
- array_to_string(anyarray, text [, text])
描述:使用第一个text作为数组的新分隔符,使用第二个text替换数组值为null的值。
返回类型:text
示例:
1 2 3 4 5
openGauss=# SELECT array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*') AS RESULT; result ----------- 1,2,3,*,5 (1 row)
- array_delete(anyarray)
返回类型:anyarray
示例:
openGauss=# SELECT array_delete(ARRAY[1,8,3,7]) AS RESULT; result -------- {} (1 row)
- array_deleteidx(anyarray, int)
返回类型:anyarray
示例:
openGauss=# SELECT array_deleteidx(ARRAY[1,2,3,4,5], 1) AS RESULT; result ----------- {2,3,4,5} (1 row)
- array_extendnull(anyarray, int)
返回类型:anyarray
示例:
openGauss=# SELECT array_extend(ARRAY[1,8,3,7],1) AS RESULT; result -------------- {1,8,3,7,null} (1 row)
- array_trim(anyarray, int)
返回类型:anyarray
示例:
openGauss=# SELECT array_trim(ARRAY[1,8,3,7],1) AS RESULT; result --------- {1,8,3} (1 row)
- array_exists(anyarray, int)
返回类型:boolean
示例:
openGauss=# SELECT array_exists(ARRAY[1,8,3,7],1) AS RESULT; result -------- t (1 row)
- array_next(anyarray, int)
描述:根据第二个入参返回数组中指定下标元素的下一个元素的下标。
返回类型:int
示例:
openGauss=# SELECT array_next(ARRAY[1,8,3,7],1) AS RESULT; result -------- 2 (1 row)
- array_prior(anyarray, int)
描述:根据第二个入参返回数组中指定下标元素的上一个元素的下标。
返回类型:int
示例:
openGauss=# SELECT array_prior(ARRAY[1,8,3,7],2) AS RESULT; result -------- 1 (1 row)
- string_to_array(text, text [, text])
描述:使用第二个text指定分隔符,使用第三个可选的text作为NULL值替换模板,如果分隔后的子串与第三个可选的text完全匹配,则将其替换为NULL。
返回类型:text[]
示例:
1 2 3 4 5 6 7 8 9 10
openGauss=# SELECT string_to_array('xx~^~yy~^~zz', '~^~', 'yy') AS RESULT; result -------------- {xx,NULL,zz} (1 row) openGauss=# SELECT string_to_array('xx~^~yy~^~zz', '~^~', 'y') AS RESULT; result ------------ {xx,yy,zz} (1 row)
- unnest(anyarray)
返回类型:setof anyelement
示例:
1 2 3 4 5 6
openGauss=# SELECT unnest(ARRAY[1,2]) AS RESULT; result -------- 1 2 (2 rows)
在string_to_array中,如果分隔符参数是NULL,输入字符串中的每个字符将在结果数组中变成一个独立的元素。如果分隔符是一个空白字符串,则整个输入的字符串将变为一个元素的数组。否则输入字符串将在每个分隔字符串处分开。
在string_to_array中,如果省略null字符串参数或为NULL,将字符串中没有输入内容的子串替换为NULL。
在array_to_string中,如果省略null字符串参数或为NULL,运算中将跳过在数组中的任何null元素,并且不会在输出字符串中出现。