Postgresql Json Sql
a detailed website about json sql query; official website: here, chinese version: here
Json query: ->element_name or index; and can be sequential;
select column::json->'element' as readdata from schema.tableName; select column1::json->'element1' as readdata1, column2::json->'element2' as readdata2 from schema.tableName;
using json function to do operations.
select description::json->'data'->(JSON_ARRAY_LENGTH(description::json->'data')-1)->0 as readtime from leanheat.lh_demo_ecl_aggregatedata
Examples of json operation(增删改查):from here
CREATE TABLE leanheat.TActivity ( id bigint PRIMARY KEY,-- 活动id title character varying(128) NOT NULL,-- 活动名称 pricepackage jsonb NOT NULL -- 价格套餐,格式如:[{"packagename":"成人票","price":25,"stock":1000},{"packagename":"儿童票(12岁以下)","price":15,"stock":1000}] )WITH ( OIDS=FALSE );
insert data; and update one data in jsonb field.
insert into leanheat.TActivity values(1,'演出活动标题1','[{"packagename":"成人票","price":189,"stock":100},{"packagename":"儿童票(12岁以下)","price":66,"stock":20},{"packagename":"成人+儿童套票","price":128,"stock":10}]'); update leanheat.tactivity set pricepackage = pricepackage || '{"packagename":"成人票新增3","price":189,"stock":100}' where id = 1;
update;
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{description, data}', '{"data":[1568898000,29.232178],"data":[1568901600,29.232]}'::jsonb, true) where description::jsonb->>'series_id' = 'TE8'
jsonb_insert(
target jsonb, -> The JSONB data target.
path text[], -> The path on the target where the new_value will be inserted.
new_value jsonb, -> The new value/key to be inserted.
[insert_after boolean] -> If true, new_value will be inserted after the target path.
And before, if it´s set to false. Default is false.
)
update leanheat.tactivity set pricepackage = jsonb_set(pricepackage,'{0}','{"price":189,"packagename":"成人票00","stock":100}',false) where id = 1
update leanheat.tactivity set pricepackage = jsonb_set(pricepackage,'{0,packagename}','"成人票000"',false) where id = 1
delete content of tables.
delete from leanheat.tactivity; --delete the content of table. insert into leanheat.TActivity values(1,'演出活动标题1','[{"packagename":"成人票","price":189,"stock":100},{"packagename":"儿童票(5-15岁)","price":66,"stock":20},{"packagename":"成人+儿童套票","price":128,"stock":10}]'); insert into leanheat.TActivity values(2,'演出活动标题2','[{"packagename":"成人票","price":99,"stock":100},{"packagename":"儿童票(3-5岁)","price":58,"stock":20},{"packagename":"成人+儿童套票","price":99,"stock":10}]');
drop table;
DROP TABLE table_name; --drop table.
Indexing; 简介
CREATE INDEX index_name ON table_name (id); -- create index in a column (id).
要删除索引,请使用 DROP INDEX
命令。可以随时向表中添加索引或从表中删除索引。
One problem of jsonb_insert();
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{data}', '{"data":[[1568898000,29.232178],[1568901600,29.232]]}'::jsonb, true) where description::jsonb->>'series_id' = 'TE8'
ERROR: cannot replace existing key HINT: Try using the function jsonb_set to replace key value. 因为已经有data的key了,所以不能再插入key为data的数据了。
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{data,0}', '{"data":[[1568898000,29.232178],[1568901600,29.232]]}'::jsonb, true) where description::jsonb->>'series_id' = 'TE8'
改变路径,则会变为以下结果:
加引号的方式,会为如下后面的结果。
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{data,0}', '"[1568898000,29.232178],[1568901600,29.232]"'::jsonb, true) where description::jsonb->>'series_id' = 'TE8'
改变插入方式为插入数组。
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{data,0}', to_jsonb(ARRAY[[1568898000,29.232178],[1568901600,29.232]]), true) where description::jsonb->>'series_id' = 'TE9'
只插入一个数组,成功!
update leanheat.lh_demo_ecl_aggregateData set description = jsonb_insert(description, '{data,0}', to_jsonb(ARRAY[1568898000,29.232178]), true) where description::jsonb->>'series_id' = 'TE9'