CREATE TABLE arr_test (
name text,
color varchar[],
size varchar[]
);
INSERT INTO arr_test
VALUES ('adult_f','{"grey","black","white"}','{"s","m","l","xl","xxl"}'),
('adult_w','{"red","black","white","grey","blue"}','{"s","m","l","xl"}'),
('child_a','{"red","black","green","pink"}','{"s","m","l"}'),
('child_b','{}','{"m","l"}'), --空数组
('child_c',Null,'{"m"}'),
('child_d',array[''],Null),--数组中有一个元素,是空字符串
('test','{""}',null)--数组中有一个元素,是空字符串
可通过下标进行访问:fileName[int],若指定的下标越界或者数组本身为空,返回null
需要注意的是:空数组和数组中有一个空字符串的区别,实际查看库的时候看着都是空的,但是空数组计算的长度是0,有一个空字符串的数组计算长度是1
--下标访问,从1开始
SELECT name,color[1] FROM arr_test;
-------运行结果-----------
# name color
1 adult_f grey
2 adult_w red
3 child_a red
4 child_b [NULL]
5 child_c [NULL]
6 child_d
7 test
--切片(如果下标错误或者数组值本身为空,返回的是空值(null)
SELECT name,color[1:4] FROM arr_test;
-------运行结果-----------
# name color
1 adult_f {grey,black,white}
2 adult_w {red,black,white,grey}
3 child_a {red,black,green,pink}
4 child_b {}
5 child_c NULL
6 child_d {}
7 test {}
array_length
--array_length返回一个指定数组维度的长度,1表示第一维度上数组的长度,如果指定的维度不存在或者是空数组则返回null
SELECT name,array_length(color,1) FROM arr_test;
-------运行结果-----------
# name array_length
1 adult_f 3
2 adult_w 5
3 child_a 4
4 child_b [NULL]
5 child_c [NULL]
6 child_d 1
7 test 1
array_to_string
--array_to_string将color字段转为字符串,以逗号分隔
select name,array_to_string(color,',') from arr_test
------运行结果---------
# name array_to_string
1 adult_f grey,black,white
2 adult_w red,black,white,grey,blue
3 child_a red,black,green,pink
4 child_b
5 child_c [NULL]
6 child_d
7 test
array_position
和array_positions
--array_position:在一个 数组中搜索特定值,返回第一次出现的位置,若不存在返回空值
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
------运行结果---------
# array_position
1 2
--array_positions:在一个 数组中搜索特定值,返回出现的位置,若不存在返回{}
SELECT array_positions(ARRAY['sun','mon','tue','wed','thu','fri','mon'], 'mon');
------运行结果---------
# array_positions
1 {2,7}
SELECT array_positions(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'm9on');
------运行结果---------
# array_positions
1 {}
操作符 | 描述 | 例子 | 结果 | |
---|---|---|---|---|
= | 等于 | ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3] | t | |
<> | 不等于 | ARRAY[1,2,3] <> ARRAY[1,2,4] | t | |
< | 小于 | ARRAY[1,2,3] < ARRAY[1,2,4] | t | |
> | 大于 | ARRAY[1,4,3] > ARRAY[1,2,4] | t | |
<= | 小于等于 | ARRAY[1,2,3] <= ARRAY[1,2,3] | t | |
>= | 大于等于 | ARRAY[1,4,3] >= ARRAY[1,4,3] | t | |
@> | 包含 | ARRAY[1,4,3] @> ARRAY[3,1,3] | t | |
<@ | 被包含 | ARRAY[2,2,7] <@ ARRAY[1,7,4,2,6] | t | |
&& | 重叠 ,有相同的元素 | ARRAY[1,4,3] && ARRAY[2,1] | t | |
|| | 数组和数组连接 | ARRAY[1,2,3] ||ARRAY[4,5,6] | t | 等同array_cat(anyarray, anyarray) |
|| | 数组到元素连接 | ARRAY[4,5,6] || 7 | t | 等同array_append(anyarray, anyelement) |
|| | 元素到数组连接 | 3 || ARRAY[4,5,6] | t | 等同array_prepend(anyelement, anyarray) |
select * from arr_test where array['red']&&color
------运行结果---------
org.jkiss.dbeaver.model.sql.DBSQLException: SQL 错误 [42883]: 错误: 操作符不存在: text[] && character varying[]
建议:No operator matches the given name and argument types. You might need to add explicit type casts.
根据错误信息是两边数组类型不一样,需要进行转换,可修改为以下几种方式:
#方式一
select * from arr_test where array['red']::character varying[]&&color
#方式二
select * from arr_test where cast(array['red'] as varchar[])&&color
#方式三
select * from arr_test where '{red,blue}'&&color