postgresql資料庫對json資料的處理
阿新 • • 發佈:2019-02-06
1. 兩種資料形式儲存 json,jsonb
(1)json儲存快,使用慢; 存的時候不做處理,使用時再解析
(2)jsonb儲存稍慢,儲存時就做了解析,使用時速度較快
(3)兩者的部分函式很相似,稍有區別
2.使用例子
(1)建立學生表
CREATE TABLE if not exists public.student
(
name varchar,
user_card varchar,
age int,
info jsonb,
CONSTRAINT student_pkey PRIMARY KEY (user_card)
);
insert into public.student values('tom', '1234567890', 11,'{"key1":[1,2,4,["a","b","c"]],"key2":[2,5,7,["a","d","e"]]}');
insert into public.student values('katty', '1234509890', 20,'{"key1":[8,10,4,["j","n","c"]],"key2":[9,5,19,["a","m","e"]]}');
(2)根據條件獲取json
select info from public.student where name='tom'
+————————————————————————–+
| info |
+————————————————————————–+
| {“key1”: [1, 2, 4, [“a”, “b”, “c”]], “key2”: [2, 5, 7, [“a”, “d”, “e”]]} |
+————————————————————————–+
(3) 根據key獲取資料
select info->'key1' as info_list from public.student;
+—————————–+
| info_list |
+—————————–+
| [1, 2, 4, [“a”, “b”, “c”]] |
| [8, 10, 4, [“j”, “n”, “c”]] |
+—————————–+
2 rows in set