Postgresql中的jsonb数据类型学习使用

pgsql是在9.2版本中引入了对于json的支持
一般情况下使用到json数据格式的时候,99%都是查询,所以接下来看一下项目中对于json类型的数据是如何查询的

定义表

sql 复制代码
CREATE TABLE tv_sup_sl_dw_query (
  id varchar(200)  NOT NULL,
  sup_record_id varchar(100)  default NULL,
  create_org_id varchar(100)  default NULL,
  create_org_name varchar(100)  default NULL,
  create_time timestamp(6),
  party_id varchar(100)  default NULL,
  party_name varchar(100)  default NULL,
  party_sup_from varchar(100)  default NULL,
  party_sup_from_key varchar(100)  default NULL,
  //表中的数据类型就是jsonb
  party_sup_other_json jsonb
);

对应的实体模型

java 复制代码
@TableDefine(name = "tv_sup_sl_dw_query", abstractCol = "_abstract_col")
public class TV_SUP_DW_QUERY_DS {

    @ColumnDefine(length=200,pk=true)
    protected String id;
    @ColumnDefine(length=100)
    protected String sup_record_id;
    protected Date create_time;
    @ColumnDefine(length=100)
    protected String create_org_id;
    @ColumnDefine(length=100)
    protected String create_org_name;
    @ColumnDefine(length = 100)
    protected String sp_type;
    @ColumnDefine(length=100)
    protected String party_id;
    @ColumnDefine(length=100)
    protected String party_name;
    @ColumnDefine(length=100)
    protected String party_org_type;
    @ColumnDefine(length=100)
    protected String party_sup_from;
    @ColumnDefine(length=100)
    protected String party_sup_from_key;
    @ColumnDefine(sqlType=DataSetDefine.TYPE_JSONB)
    protected String party_sup_other_json;
    //TODO 省略构造方法,get/set方法
}
//这个PartySupRecordOtherJsonDTO 就是party_sup_other_json存储的内容,将PartySupRecordOtherJsonDTO 对象转成json字符串赋值给party_sup_other_json即可
public class PartySupRecordOtherJsonDTO {
    
    private String process_type;
    private String q_process_type;
    private String content_abstract;
    private String comment;
    private String handle_opinion;
    private String disput_content;
    private String work_plan;
    private int need_blqkbg;
    private int l_uploadsj;
    private String childPartyIds;
    private String childPartyNames;
    private String hasMoreChildParties;
    private String childPartyBlqkbg;
    private List<Map<String,Object>> persons;
}

对于简单数据类型的查询

sql 复制代码
select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'childPartyBlqkbg'!='1'

一下这些sql 都是在Java代码中写的

对于复杂数据类型的查询

sql 复制代码
--jsonb_array_elements  这个函数就是将persons集合转成一个对象数组
select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'persons' is not null and dw.party_sup_other_json ->> 'persons' != '[]' and exists (select 1 from jsonb_array_elements ( dw.party_sup_other_json -> 'persons') ps where 1=1 and ps ->> 'sex' = '1' and ps ->> 'name' like '%立%')

--这里的jsonb_array_elements  也可以换成 jsonb_to_recordset函数
select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'persons' is not null and dw.party_sup_other_json ->> 'persons' != '[]' and exists (select 1 from jsonb_to_recordset( dw.party_sup_other_json -> 'persons') ps(sex text,name text) where 1=1 and ps ->> 'sex' = '1' and ps ->> 'name' like '%立%')
-- ps(sex text,name text) 这里必须要写,条件中使用到了那个字段,这里就必须要定义,可以定义多个,但不能少,比如ps(sex text,name text,phone text) 这样的。如果少定义了则会报错。这里的字段就是数据库表中的字段,text 代表字符串,integer 代表int

其他一些函数的使用

sql 复制代码
select id from tv_sup_sl_dw_query dw where dw.date_part('day',tv.create_time\:\:timestamp - :nowDate \:\:timestamp) >= 2
-- date_part这个函数是获取两个时间之间的天数

jsonb中的数据也可以获取,作为查询列

sql 复制代码
select id,dw.party_sup_other_json ->> 'comment',dw.party_sup_other_json ->> 'content_abstract' from tv_sup_sl_dw_query where dw.create_time >= '2024-01-01 00:00:00'

其他函数可以自行百度或问文心一言

相关推荐
倔强的石头_1 天前
《Kingbase护城河》——数据库存储空间全景探测与精细化瘦身实战
数据库
冬奇Lab2 天前
每日一个开源项目(第134篇):Zvec - 阿里开源的嵌入式向量数据库,向量搜索界的 SQLite
数据库·人工智能·llm
ClouGence2 天前
Oracle CDC 架构优化:从主库直连到 DataGuard 备库同步
数据库·后端·oracle
无响应de神2 天前
三、用户与权限管理
数据库·mysql
通信小呆呆3 天前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人
H__Rick3 天前
自动对焦学习-3
人工智能·学习·计算机视觉
Daisy Lee3 天前
量化学习-第1章-什么是量化金融
学习·金融·datawhale
Alsn863 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker
YM52e3 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
麦聪聊数据3 天前
数据服务化时代:企业数据能力输出的核心路径
数据库