PostgreSQL -- 数组类型操作
admin
2023-05-11 07:02:13
0

一、数组类型创建表

数组类型,要求数组内的元素属于同一种类型,当出现No function matches the given name and argument types. You might need to add explicit type casts.报错的时候,说明 list 的格式和插入数据或者修改数据的格式不同导致的, 类型很重要,需要保证类型相同才可以操作
 

1.1、建表指定数组类型

只需要在表字段类型后面加'[]'

postgres=# create table test1 (
    id serial, 
    arr int[], 
    name varchar(10)[], 
    age char(10)[], 
    score float[]
    ); 

postgres=# \d+ test1;
                                                 Table "public.test1"
     Column |          Type           |                     Modifiers                      | Storage  | Stats target | Description 
--------+-------------------------+----------------------------------------------------+----------+--------------+-------------
 id     | integer                 | not null default nextval('test1_id_seq'::regclass) | plain    |              | 
 arr    | integer[]               |                                                    | extended |              | 
 name   | character varying(10)[] |                                                    | extended |              | 
 age    | character(10)[]         |                                                    | extended |              | 
 score  | double precision[]      |                                                    | extended |              | 

 

1.2、数据插入

postgres=# insert into test(id, uid) values(3, '{1, 2, 3}');        插入数组方式1
postgres=# insert into test(id, uid) values(3, array[20, 30]::int8[]);   插入数组方式二

1.3、修改数组:

postgres=# update test set uid = uid || '{0}';    后面追加一个数组
postgres=# update test set uid='{0,0}' || uid;   在前面插入一个数组
postgres=# update arr_test set uid=array_append(uid, '1'::int);   指明类型追加一个数
postgres=# update arr_test set uid=array_append(uid, 1);   按默认int类型追加一个数
postgres=# update arr_test set uid=array_prepend('1'::int, uid);     在前面插入一个数

1.4、删除数组中的数据

postgres=# update arr_test set uid=array_remove(uid, '1'::int);  指明类型移除指定的数

1.5、查找数组中的数据

postgres=# select * from test where 20=any(uid);    #uid数组中存在20的row
postgres=# select * from test where uid && array[20, 1]::int8[];   uid数组中和array[20, 1]存在交集的
postgres=# select * from arr_test where uid@>'{1, 2}';   uid 数组中同时包含[1, 2]的
postgres=# select * from arr_test where uid<@'{1, 2}';   uid 数组被[1, 2]包含的

postgres=# select * from arr_test where 2=uid[1]; 使用uid 数组下标查询,下标是从1开始的
postgres=# select id, uid[2] from arr_test; 使用下标显示

相关内容

热门资讯

河南一企业每月给员工父母发“工... 近日,河南一企业每月1日给员工父母发“工资”一事在网上引热议,很多人点赞,更多也是好奇:咋发?发多少...
“90后夫妻同患罕见病瘫痪”引... 近日,一段“90后新婚夫妻同患罕见病瘫痪”的视频在网上流传,引发关注和热议。有网友对夫妻俩的遭遇表示...
北京:每年开放场景空间资源和场... 5月13日,北京市发展改革委、北京市科委、中关村管委会等部门联合发布了《北京市加快推动场景培育和开放...
DAA衡量智能体的时代,云服务... 今年以来,国内外头部云厂商相继上调部分核心产品价格,打破了行业近二十年“只降不升”的惯例。这背后,是...
2026年网络发稿平台权威测评... 引言:数字化浪潮下,网络发稿生态正在经历深刻变革 在信息爆炸的互联网时代,网络空间已成为企业品牌传播...
“小皮球,架脚踢,马兰开花二十... “二八二五六,二八二五七,二八二九三十一……”当短视频里的外国网友惊呼“这是什么暗号”时,有网友笑称...
房子重新装修需要多少钱(房子重... 像新房子装修后的使用寿命大概在15到20年的样子,之后的各种材料都因时间的关系会逐渐老化,看着越来越...
不锈钢橱柜多少一米 具体要看你使用的不锈钢是哪种型号的,因为市面上有很多种型号的不锈钢,当然价钱也有所不同。那么我们普通...
300-300的瓷砖是多少平方 我们在计算平方的时候,都是将长度乘以宽度,所以300×300的瓷砖,这些瓷砖的面积肯定是0.9个平方...
热水器是一直开着省电还是现用现... 一般情况下家用的热水器是现用现烧比较省电,当热水器一直开着时,热水器会一直持续进行烧水工作,这样会比...