postgresql json数据
参考文章:
JSON数据操作
操作符
1、-> 表示获取一个JSON数组元素,支持下标值(下标从0开始)、Key获取。
2、->> 表示获取一个JSON对象字符串。
3、#> 表示获取指定路径的一个JSON对象。
4、#>>表示获取指定路径的一个JSON对象的字符串。
1、版本
select version();
2、数据准备
-
创建学生表
CREATE TABLE students ( stu_id SERIAL primary key , student_desc jsonb ); -- 插入数据 INSERT INTO "students" ("stu_id", "student_desc") VALUES (1, '{"date": "2024-10-12", "sdata": {"sno": "1001", "tel": ["010-82886998", "13550011000"], "addr": ["北京市海淀区科学大厦", "海淀区中关村路2号"], "sage": "23", "ssex": "女"}, "sname": "李思佳", "courses": [{"cno": "01", "cname": "math", "teacher": {"tno": "101", "tname": "刘平安"}}, {"cno": "02", "cname": "chinese", "teacher": {"tno": "102", "tname": "李昌阳"}}, {"cno": "03", "cname": "english", "teacher": {"tno": "103", "tname": "Steven"}}]}'); INSERT INTO "students" ("stu_id", "student_desc") VALUES (2, '{"sc": [{"cno": "01", "score": "76"}, {"cno": "02", "score": "77"}, {"cno": "03", "score": "87"}], "date": "2024-10-12", "sdata": {"sno": "1004", "tel": ["021-2861789", "18211028796"], "addr": ["科技一路6号", "科技二路8号", "科技三路20号"], "sage": "20", "ssex": "女"}, "sname": "张成", "courses": [{"cno": "01", "cname": "math", "teacher": {"tno": "101", "tname": "刘平安"}}, {"cno": "02", "cname": "chinese", "teacher": {"tno": "102", "tname": "李昌阳"}}, {"cno": "03", "cname": "english", "teacher": {"tno": "103", "tname": "Steven"}}]}');
-
学生成绩表
CREATE TABLE sc ( "sno" text COLLATE "pg_catalog"."default" NOT NULL, "cno" text COLLATE "pg_catalog"."default" NOT NULL, "score" text COLLATE "pg_catalog"."default", CONSTRAINT "sc_pkey" PRIMARY KEY ("sno", "cno") ); -- 插入数据 insert into sc values('1001','01','99'); insert into sc values('1001','02','89'); insert into sc values('1001','03','77'); insert into sc values('1002','01','45'); insert into sc values('1002','02','78'); insert into sc values('1002','03','100'); insert into sc values('1003','01','90'); insert into sc values('1003','02','76'); insert into sc values('1003','03','87');
3、查询
3.1获取一个JSON对象字符串
-- ->>表示获取一个JSON对象字符串。
select student_desc ->> 'sdata' as stu_data,
student_desc ->> 'sname'as stu_name
from students where stu_id = 1
3.2获取一个JSON数组元素
-- -> 表示获取一个JSON数组元素,支持下标值(下标从0开始)、Key获取。
select student_desc->'sdata' as stu_data from students;
3.3获取一个JSON对象内数据
-- 获取对象 -> 返回值带引号
select student_desc->'sdata'->'sage' as age from students;
-- 获取值 ->> 直接返回值
select student_desc->'sdata'->>'sage' as age from students;
3.4获取一个JSON对象内数组
-- 获取数组对象
select student_desc->'sdata'#>'{addr}' as addr from students;
-- 获取数组对象值 指定下标
select student_desc->'sdata'#>>'{addr,1}' as addr from students;
3.5 获取一个JSON数组对象
-- #> 表示获取指定路径的一个JSON对象
-- 获取整个数组对象
select student_desc #>'{courses}' as stu_courses from students;
-- 获取数组对象,指定下标
select student_desc #>'{courses,0}' as stu_courses from students;
3.6获取一个JSON数组内对象内的数据
-- 获取数组内对象
select student_desc #>'{courses,0}'->'teacher' as teacher from students;
-- 获取数组内对象中数据
select student_desc #>'{courses,0}'->'teacher'->>'tname' as stu_courses from students;
4、增改
新增一个属性
update students set student_desc=student_desc|| '{"nickname":"平安"}'
where stu_id=1;
删除一个属性
update students set student_desc = student_desc - 'nickname' where stu_id=1;
删除所有
update students set student_desc = student_desc - 'sdata' where stu_id=1;
5、联合查询
表转JSON
--
select row_to_json(sc.*) from sc where sno='1001';
联表查询
-- 联表查询
select student_desc->>'sname' as stu_name,student_desc #>'{courses}' as stu_courses from sc
join students s on text(s.student_desc->'sdata'->>'sno')=sc.sno and sc.sno='1001' and sc.cno='01';