PostgreSQL学习篇9.17 数组类型
来源:互联网 发布:在线数据挖掘平台 编辑:程序博客网 时间:2024/05/29 18:35
数组类型声明:create table testsz(id int,col1 int[]);输入数组值:postgres=# create table testsz(id int,col1 int[]);CREATE TABLEpostgres=# insert into testsz values(1,'{1,2,3}');INSERT 0 1postgres=# insert into testsz values(2,'{4,5,3}');INSERT 0 1postgres=# select * from testsz; id | col1 ----+--------- 1 | {1,2,3} 2 | {4,5,3}(2 rows)postgres=#数组访问:postgres=# select id,col1[1] from testsz; id | col1----+------ 1 | 1 2 | 4(2 rows)postgres=#数组值修改:postgres=# select * from testsz; id | col1 ----+--------- 1 | {1,2,3} 2 | {4,5,3}(2 rows)postgres=# update testsz set col1='{9,11,13}' where id=1;UPDATE 1postgres=# select * from testsz; id | col1 ----+----------- 2 | {4,5,3} 1 | {9,11,13}(2 rows)postgres=# update testsz set col1[3]=19 where id=1;UPDATE 1postgres=# select * from testsz; id | col1 ----+----------- 2 | {4,5,3} 1 | {9,11,19}(2 rows)postgres=#重点介绍两个函数:unnest(anyarray):把数组变成多行返回;array_agg():数组聚合示例:、postgres=# create table testfun(id int,v int);CREATE TABLEpostgres=# insert into testfun values(1,1);INSERT 0 1postgres=# insert into testfun values(1,2);INSERT 0 1postgres=# insert into testfun values(1,3);INSERT 0 1postgres=# insert into testfun values(2,3);INSERT 0 1postgres=# insert into testfun values(2,1);INSERT 0 1postgres=# insert into testfun values(2,2);INSERT 0 1postgres=# insert into testfun values(2,3);INSERT 0 1postgres=# insert into testfun values(3,3);INSERT 0 1postgres=# insert into testfun values(3,5);INSERT 0 1postgres=# select * from testfun; id | v----+--- 1 | 1 1 | 2 1 | 3 2 | 3 2 | 1 2 | 2 2 | 3 3 | 3 3 | 5(9 rows)postgres=# select id,array_agg(v) from testfun group by id; id | array_agg----+----------- 1 | {1,2,3} 3 | {3,5} 2 | {3,1,2,3}(3 rows)postgres=# select * from testsz; id | col1 ----+----------- 2 | {4,5,3} 1 | {9,11,19}(2 rows)postgres=# select id,unnest(col1) from testsz; id | unnest----+-------- 2 | 4 2 | 5 2 | 3 1 | 9 1 | 11 1 | 19(6 rows)postgres=#
0 0
- PostgreSQL学习篇9.17 数组类型
- postgresql 数组类型
- PostgreSQL学习篇9.1 布尔类型
- PostgreSQL学习篇9.2 数值类型
- PostgreSQL学习篇9.3 浮点数类型
- PostgreSQL学习篇9.4 序列类型
- PostgreSQL学习篇9.5 货币类型
- PostgreSQL学习篇9.7 字符串类型
- PostgreSQL学习篇9.8 位串类型
- PostgreSQL学习篇9.9 日期/时间类型
- PostgreSQL学习篇9.10 枚举类型
- PostgreSQL学习篇9.11 几何类型
- PostgreSQL学习篇9.12 网络地址类型
- PostgreSQL学习篇9.13 复合类型
- PostgreSQL学习篇9.14 XML类型
- PostgreSQL学习篇9.15 JSON类型
- PostgreSQL学习篇9.16 range类型
- PostgreSQL学习篇9.3 浮点数类型
- Runtime.getRuntime().exec执行阻塞问题解决 .
- [Qt5] 减少dll依赖和大小(特别是webkit的大小和依赖) - 一 水
- 【C语言】LeetCode 3. Longest Substring Without Repeating Characters
- spring自定义标签如何实现
- spring通过注解配置
- PostgreSQL学习篇9.17 数组类型
- oracle索引
- js简单的验证码测试
- ‘8 reasons I accepted your article'
- 友盟推送的详细过程
- Linux 定时任务crontab
- 采用devtools实现热部署
- android IBeacon 开发(上)搜索IBeacon基站
- 支付宝 支付集成 报错(一)