- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章postgres array_to_string和array的用法讲解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
有三张表,分别如下:
1
|
select
*
from
vehicle
|
1
|
select
*
from
station
|
1
|
select
*
from
vehicle_station
|
需求:
vehicle和station表示多对多的关系,需要把vehicle表对应的station表的第二字段查出来放到一个字段,如果对应多条,用逗号隔开放到一个字段.
解决方案:
1
|
SELECT
v.*, array_to_string(ARRAY (
SELECT
station_name
FROM
station
WHERE
ID
IN
(
SELECT
station_id
FROM
vehicle_station
WHERE
vehicle_id = v. ID)),
','
) station_names
FROM
vehicle v
|
结果如下:
补充:Postgres array 数组类型详细使用 。
德哥这篇文章写的很不错,在相关函数部分,尤其是 array_upper,array_lower 部分,有我自己的一些解释.
ARRAY类型包含几个重要的特征 。
维度 。
也就是几维数组, 不管怎么赋值, ARRAY最终必须是个矩阵. 。
例1
ARRAY[1,2,3,4] 是一维数组.
ARRAY[[1,2],[3,4],[5,6]] 是二维数组 。
例2
ARRAY[['digoal','zhou'],['a','b',c']] 是错误的. 因为第二个维度中的第一个array有2个元素, 而第二个array有3个元素. 不是一个矩阵. 个数必须一致. 。
同时类型也必须一致 。
例3
ARRAY[['digoal','zhou'],[1,2]] 是错误的. 因为['digoal','zhou']是text[]类型, 而[1,2]是int[]类型. 。
元素 。
一维数组ARRAY[1,2,3,4] 中的4个元素分别是 1, 2, 3, 4. 这些int型的值. 。
二维数组ARRAY[[1,2],[3,4],[5,6]] 中的第一维度有3个元素是 ARRAY[1,2] , ARRAY[3,4] , ARRAY[5,6] 这些int[]类型的值. 第二个维度的第一个subscript的元素有两个, 分别是1,2 . 第二个subscript 分别是3,4. 第三个subscript分别是5,6. 。
元素之间的分隔符, 除了box类型是分号;, 其他类型的分隔符都是逗号,. 。
扩展性 。
一维数组可以扩展, 二维数组无法扩展. 。
subscript 。
访问ARRAY中的元素需要提供subscript值. 默认是从1开始编号. 除非赋值的时候强制指定subscript 。
例1
1
2
3
4
5
6
7
|
ARRAY[[1,2],[3,4],[5,6]]
as
a
a[1][1] = 1;
a[1][2] = 2;
a[2][1] = 3;
a[2][2] = 4;
a[3][1] = 5;
a[3][2] = 6;
|
a第一个[]表示第一维度, 里面的数字代表第一维度中要访问的subscript.
a第二个[]表示第二维度, 里面的数字代表第二维度中要访问的subscript.
另外也可以访问ARRAY的slice. 。
例2
1
|
a[1:2][1:1] = {{1},{3}}
|
第一个[]中的1表示低位subscript, 2表示高位subscript值. 。
第二个[]中左边的1表示低位subscript, 右边的1表示高位subscript值. 。
1
|
a[2:3][1:2] = {{3,4},{5,6}}
|
分片的另一种写法, 只要其中的一个维度用了分片写法, 其他的维度如果没有使用分片写法, 默认视为高位 。
如a[2:3][2] 等同于 a[2:3][1:2] 。
接下来讲解一下ARRAY类型的几个常用函数
array_dims, 返回的是各个维度中的低位subscript和高位subscript, 如下
1
2
3
4
|
digoal=>
select
array_dims(ARRAY[[1,2,3,4,5],[6,7,8,9,10]]);
array_dims
---------+--
[1:2][1:5]
|
array_length, 返回的是array中指定维度的长度或元素个数, 如下
1
2
3
4
5
6
7
8
|
digoal=>
select
array_length(ARRAY[[1,2,3,4,5],[6,7,8,9,10]], 1);
array_length
-------+------
2
digoal=>
select
array_length(ARRAY[[1,2,3,4,5],[6,7,8,9,10]], 2);
array_length
--------+-----
5
|
注意:array_lower 和 array_upper 返回值都是下标 ,默认从1开始的下标.
array_lower, 返回的是ARRAY中指定维度的低位subscript值, 如下
1
2
3
4
|
digoal=>
select
array_lower(ARRAY[[1,2,3,4,5],[6,7,8,9,10]], 2);
array_lower
--------+----
1
|
下面就是强制指定subscript值了.
1
2
3
4
|
digoal=>
select
array_lower(
'[-3:-2]={1,2}'
::
int
[], 1);
array_lower
---------+----
-3
|
array_upper, 返回的是ARRAY中指定维度的高位subscript值, 如下
1
2
3
4
|
digoal=>
select
array_upper(ARRAY[[1,2,3,4,5],[6,7,8,9,10]], 2);
array_upper
--------+----
5
|
下面就是强制指定subscript值了.
1
2
3
4
|
digoal=>
select
array_upper(
'[-3:-2]={1,2}'
::
int
[], 1);
array_upper
--------+----
-2
|
array_prepend, 用于在一维数组的前面插入元素, 如下 。
1
2
3
4
5
6
7
8
9
|
digoal=>
select
array_prepend(
'digoal'
, ARRAY[
'francs'
,
'david'
]);
array_prepend
-------------------+---
{digoal,francs,david}
array_append, 用于在一维数组的后面插入元素, 如下
digoal=>
select
array_append(ARRAY[
'francs'
,
'david'
],
'digoal'
);
array_append
---------------+-------
{francs,david,digoal}
|
array_cat, 用于两个相同维度的数组的连接, 或者一个n维数组和一个n+1维数组的连接, 如下 。
1
2
3
4
5
6
7
8
|
digoal=>
select
array_cat(ARRAY[
'francs'
], ARRAY[
'digoal'
,
'david'
]);
array_cat
----------------+------
{francs,digoal,david}
digoal=>
select
array_cat(ARRAY[
'francs'
], ARRAY[[
'digoal'
]]);
array_cat
-----------------+---
{{francs},{digoal}}
|
generate_subscripts, 用于按顺序返回ARRAY的指定维度的subscript(s)值, 如下
正向返回第一维度的subscript值. 。
1
2
3
4
5
6
7
|
digoal=>
select
generate_subscripts(a, 1)
from
(
select
ARRAY[
'a'
,
'b'
,
'c'
,
'd'
]
as
a) t;
generate_subscripts
---------------+-----
1
2
3
4
|
反向返回第一维度的subscript值. 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
digoal=>
select
generate_subscripts(a, 1,
true
)
from
(
select
ARRAY[
'a'
,
'b'
,
'c'
,
'd'
]
as
a) t;
generate_subscripts
-----------------+--
4
3
2
1
digoal=>
select
generate_subscripts(a, 1)
from
(
select
'[-5:-1]={1,2,3,4,5}'
::
int
[]
as
a) t;
generate_subscripts
---------------+-----
-5
-4
-3
-2
-1
digoal=>
select
generate_subscripts(a, 1,
true
)
from
(
select
'[-5:-1]={1,2,3,4,5}'
::
int
[]
as
a) t;
generate_subscripts
---------------+-----
-1
-2
-3
-4
-5
|
多维数组的第二维度.
1
2
3
4
5
6
|
digoal=>
select
generate_subscripts(a, 2)
from
(
select
'[-5:-4][2:4]={{1,2,3},{4,5,6}}'
::
int
[]
as
a) t;
generate_subscripts
---------------+-----
2
3
4
|
接下来讲解一下ARRAY类型的操作符 。
1
2
3
4
|
digoal=>
select
typname,oid
from
pg_type
where
typname=
'anyarray'
;
typname | oid
----------+------
anyarray | 2277
|
操作符如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
digoal=>
select
oprname,oprleft,oprright,oprresult,oprcode,oprrest,oprjoin
from
pg_operator
where
oprleft=2277
or
oprright=2277;
oprname | oprleft | oprright | oprresult | oprcode | oprrest | oprjoin
---------+---------+----------+-----------+----------------+-------------+-----------------
|| | 2277 | 2283 | 2277 | array_append | - | -
|| | 2283 | 2277 | 2277 | array_prepend | - | -
|| | 2277 | 2277 | 2277 | array_cat | - | -
= | 2277 | 2277 | 16 | array_eq | eqsel | eqjoinsel
<> | 2277 | 2277 | 16 | array_ne | neqsel | neqjoinsel
< | 2277 | 2277 | 16 | array_lt | scalarltsel | scalarltjoinsel
> | 2277 | 2277 | 16 | array_gt | scalargtsel | scalargtjoinsel
<= | 2277 | 2277 | 16 | array_le | scalarltsel | scalarltjoinsel
>= | 2277 | 2277 | 16 | array_ge | scalargtsel | scalargtjoinsel
&& | 2277 | 2277 | 16 | arrayoverlap | areasel | areajoinsel
@> | 2277 | 2277 | 16 | arraycontains | contsel | contjoinsel
<@ | 2277 | 2277 | 16 | arraycontained | contsel | contjoinsel
(12
rows
)
|
【注意】 。
- PostgreSQL中对ARRAY类型的维度没有限制, 如int[]并不代表只能存储一维数组, 其实可以存储任意维度的ARRAY值. 。
- PostgreSQL中对ARRAY类型中元素的个数也没有限制, 如int[10] , 不代表只能存储10个元素.可以超出. 。
例如
1
2
3
4
|
digoal=>
create
table
array_test (id
int
[2]);
CREATE
TABLE
digoal=>
insert
into
array_test
values
(ARRAY[[1,2,3,4,5],[6,7,8,9,10]]);
INSERT
0 1
|
这个例子中元素的个数和维度都超出了int[2]的限制,但是并没有报错,而且数据已经存储进去了. 。
1
2
3
4
|
digoal=>
select
*
from
array_test ;
id
---------------------+------
{{1,2,3,4,5},{6,7,8,9,10}}
|
手册上的解释如下
However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length. 。
The current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; it does not affect run-time behavior. 。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我。如有错误或未考虑完全的地方,望不吝赐教.
原文链接:https://blog.csdn.net/ying_maxwell/article/details/72962891 。
最后此篇关于postgres array_to_string和array的用法讲解的文章就讲到这里了,如果你想了解更多关于postgres array_to_string和array的用法讲解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
ARRAY_TO_STRING()适用于字符串数组,但不支持整数数组。 我有一个包含 ID 列表的重复列,我想将其从美国数据集导出到欧盟数据集。到目前为止,我正在使用 bq query --nouse
运行下面的查询后 SELECT ARRAY_TO_STRING( reporteeNames, '|') FROM `appsflow-dev.PersonRe
在选择中,我使用了这样的 array_to_string(示例) array_to_string(array_agg(tag_name),';') tag_names 我得到了结果字符串 "tag1;
我试图在 redshift 中运行带有联合的 array_to_string,但我不断收到以下错误消息: ERROR: Specified types or functions (one per IN
我正在使用 PostgreSQL 9.5。 我在 PL/pgSQL 中创建一个触发器,当对第二个表执行 INSERT 时,它会向表 (synthese_poly) 添加一条记录 (operation_
你好,我正在编写一个 pgsql 函数,在这个函数中,我有一个使用 array_to_string 方法的请求。 AND id NOT IN (array_to_string(excludeA
我正在尝试在 Postgres 查询中组合行并连接两列(名称、vorname)。 这样效果很好: SELECT nummer, array_to_string(array_agg(nam
我在 posts 和 tags 表之间有 2 个 n-n 关系。这是我在 Postgres 中的查询: SELECT t0.*, array_to_string(array_agg(t2.tag),
我试图找到与 PostgreSQL 函数 array 和 array_to_string 等效的 MySQL,并遇到了 this post但要求 oracle9i 这对我没有帮助。我需要用 MySQL
我有一个如下所示的 Arel 语句: vulns.project(vulns[:id], vulns[:cve_id], vulns[:seve
我想知道 MySQL 中是否有 array_agg() 和 array_to_string 的 PostgreSQL 对应物(或任何东西至少接近这个功能)我的研究试图找到与这两个类似的东西,但我没有任
我希望从在 Oracle 中返回多行的查询中返回包含逗号分隔值列表的单行,实质上是将返回的行展平为单行。 在 PostgreSQL 中,这可以使用 array 和 array_to_string 函数
我是一名优秀的程序员,十分优秀!