四、PostgreSQL常用数据类型(精简)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四、PostgreSQL常用数据类型(精简)相关的知识,希望对你有一定的参考价值。
参考技术A 备注:
(1)numeric 不指定长度,可以保留的整数位和小数位很大;
(2)numeric(6,4)表示精度(precision,所有数字位的个数)为6,标度(scale,小数点右边所有小数位的个数)为4,例如23.5141
备注:varchar不指定长度,可以存储最大长度(1GB)
备注:保留两位小数,超过位数采用四舍五入法进行截断
1. https://www.yiibai.com/postgresql/postgresql-datatypes.html
PostgreSQL JSON类型字段常用操作
初始数据
insert into name_age values('"id":1,"name":"小明", "age":18');
insert into name_age values('"id":2,"name":"小明1", "age":17');
insert into name_age values('"id":3,"name":"小明3", "age":16');
insert into name_age values('"id":4,"name":"小明4", "age":20');
insert into name_age values('"id":5,"name":"小明5", "age":30');
insert into name_age values('"id":6,"name":"小明6", "age":10');
insert into name_age values('"id":7,"name":"小明7", "age":15');
insert into name_age values('"id":8,"name":"小明8", "age":35');
insert into name_age values('"id":9,"name":"小明9", "age":35');
Where
精确匹配
SELECT info||'"gender":"男"' as a,info||'"gender":"男"'::jsonb as b,info as c from name_age where (info->>'id')::int4 = 2;
SELECT info||'"gender":"男"' as a,info||'"gender":"男"'::jsonb as b,info as c from name_age where (info->>'name') = '小明4';
模糊匹配
select * from name_age where info #>> 'name' like '%明';
select * from name_age where (info->>'name') like '%4';
排序
select * from name_age order by info->'age' desc,info->'id' asc;
select * from name_age order by info->>'age' desc,info->'id' asc;
select * from name_age order by info->>'age' desc,info->>'id' asc;
修改
增加json字段
update name_age set info = info || '"gender":"男"'::jsonb where (info->>'id')::int4 = 1;
删除json字段
update name_age set info = info-'gender' where (info->>'id')::int4 = 1;
修改json字段值
update name_age set info = info || '"age":16'::jsonb where (info->>'id')::int4 = 1;
分页
select * from name_age order by info->>'age' desc,info->>'id' asc limit 2 offset 1;
- offset 从0开始
以上是关于四、PostgreSQL常用数据类型(精简)的主要内容,如果未能解决你的问题,请参考以下文章