postgresql 数组应用


Operator
DescriptionExampleResult
=equalARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]t
<>not equalARRAY[1,2,3] <> ARRAY[1,2,4]t
<less thanARRAY[1,2,3] < ARRAY[1,2,4]t
>greater thanARRAY[1,4,3] > ARRAY[1,2,4]t
<=less than or equalARRAY[1,2,3] <= ARRAY[1,2,3]t
>=greater than or equalARRAY[1,4,3] >= ARRAY[1,4,3]t
@>containsARRAY[1,4,3] @> ARRAY[3,1]t
<@is contained byARRAY[2,7] <@ ARRAY[1,7,4,2,6]t
&&overlap (have elements in common)ARRAY[1,4,3] && ARRAY[2,1]t
||array-to-array concatenationARRAY[1,2,3] || ARRAY[4,5,6]{1,2,3,4,5,6}
||array-to-array concatenationARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]{{1,2,3},{4,5,6},{7,8,9}}
||element-to-array concatenation3 || ARRAY[4,5,6]{3,4,5,6}
||array-to-element concatenationARRAY[4,5,6] || 7{4,5,6,7}

 Array Functions

 

FunctionReturn TypeDescriptionExampleResult
array_append(anyarray, anyelement)anyarrayappend an element to the end of an arrayarray_append(ARRAY[1,2], 3){1,2,3}
array_cat(anyarray, anyarray)anyarrayconcatenate two arraysarray_cat(ARRAY[1,2,3], ARRAY[4,5]){1,2,3,4,5}
array_ndims(anyarray)intreturns the number of dimensions of the arrayarray_ndims(ARRAY[[1,2,3], [4,5,6]])2
array_dims(anyarray)textreturns a text representation of array's dimensionsarray_dims(ARRAY[[1,2,3], [4,5,6]])[1:2][1:3]
array_fill(anyelement, int[], [, int[]])anyarrayreturns an array initialized with supplied value and dimensions, optionally with lower bounds other than 1array_fill(7, ARRAY[3], ARRAY[2])[2:4]={7,7,7}
array_length(anyarray, int)intreturns the length of the requested array dimensionarray_length(array[1,2,3], 1)3
array_lower(anyarray, int)intreturns lower bound of the requested array dimensionarray_lower('[0:2]={1,2,3}'::int[], 1)0
array_prepend(anyelement, anyarray)anyarrayappend an element to the beginning of an arrayarray_prepend(1, ARRAY[2,3]){1,2,3}
array_to_string(anyarray, text [, text])textconcatenates array elements using supplied delimiter and optional null stringarray_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*')1,2,3,*,5
array_upper(anyarray, int)intreturns upper bound of the requested array dimensionarray_upper(ARRAY[1,8,3,7], 1)4
string_to_array(text, text [, text])text[]splits string into array elements using supplied delimiter and optional null stringstring_to_array('xx~^~yy~^~zz', '~^~', 'yy'){xx,NULL,zz}
unnest(anyarray)setof anyelementexpand an array to a set of rowsunnest(ARRAY[1,2])
1
2
(2 rows)

 

 

下面列举几个可能不容易查询出来的东西:

 

1. pg中查看两个数组之间有多少个重叠的数据 并且去重的方法

example :

mrapp=# SELECT  array( select UNNEST(array[1,2,3,3])         INTERSECT   SELECT
UNNEST(array[2,3,4]) );
 array
-------
 {2,3}
(1 行记录)

2. PG中没有现成的查询一个数组是否包含一个值的方法,但可以有替代方法

 

example :  查询 数组 {2,3,4,5} 中是否包含4

1> mrapp=# select array[2,3,4,5] @> array[4];
 ?column?
----------
 t
(1 行记录)

2> 自己封装函数 通过迭代循环吧

mrapp=# create or replace function array_contains_value(integer[], integer) retu
rns boolean as
mrapp-# $BODY$
mrapp$# declare
mrapp$# index integer;
mrapp$# begin
mrapp$# for index in 1..array_length($1 , 1) loop
mrapp$# if $1[index] = $2 then
mrapp$# return true;
mrapp$# end if;
mrapp$# end loop;
mrapp$# return false;
mrapp$# end;
mrapp$# $BODY$
mrapp-# language plpgsql;
CREATE FUNCTION
mrapp=# select array_contains_value(array[2,3,4,5] , 4);
 array_contains_value
----------------------
 t
(1 行记录)

 

3. 数组转字符串

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL是以加州大学伯克利分校计算机系开发的POSTGRES,现在已经更名为PostgreSQL. PostgreSQL支持大部分SQL标准并且提供了许多其它现代特性:复杂查询、外键、触发器、视图、事务完整性等。 PostgreSQL 是一个免费的对象-关系数据库服务器(数据库管理系统),它在灵活的 BSD-风格许可证下发行。它提供了相对其他开放源代码数据库系统(比如 MySQL 和 Firebird),和专有系统(比如 Oracle、Sybase、IBM 的 DB2 和 Microsoft SQL Server)之外的另一种选择。 事实上, PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统(MVCC)、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。 Inprise 的 InterBase 以及SAP等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。最后,PostgreSQL拥有一支非常活跃的开发队伍,而且在许多黑客的努力下,PostgreSQL 的质量日益提高。从技术角度来讲,PostgreSQL 采用的是比较经典的C/S(client/server)结构,也就是一个客户端对应一个服务器端守护进程的模式,这个守护进程分析客户端来的查询请求,生成规划树,进行数据检索并最终把结果格式化输出后返回给客户端。为了便于客户端的程序的编写,由数据库服务器提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如ODBC,JDBC,Python,Perl,Tcl,C/C++,ESQL等, 同时也要指出的是,PostgreSQL 对接口的支持也是非常丰富的,几乎支持所有类型的数据库客户端接口。这一点也可以说是 PostgreSQL 一大优点。 本课程作为PostgreSQL数据库管理一,主要讲解以下内容:1.     PostgreSQL安装和环境准备2.     PostgreSQL数据查询3.     PostgreSQL 数据过滤4.     PostgreSQL 多表的联接5.     PostgreSQL数据的分组6.     PostgreSQL合集的操作7.   PostgreSQL 合集的分组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值