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'

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

相关推荐
如意机反光镜裸2 分钟前
CentOS7安装MySQL教程
数据库·mysql
冰镇毛衣7 分钟前
1.4 MySql配置文件
数据库·mysql
攻城狮的梦18 分钟前
redis集群模式连接
数据库·redis·缓存
HinsCoder41 分钟前
【渗透测试】——Upload靶场实战(1-5关)
笔记·学习·安全·web安全·渗透测试·测试·upload靶场
听风若依44 分钟前
排序学习笔记
笔记·学习
Filotimo_1 小时前
【自然语言处理】实验三:新冠病毒的FAQ问答系统
人工智能·经验分享·笔记·python·学习·自然语言处理·pycharm
标贝科技1 小时前
ChatGPT对话训练数据采集渠道有哪些
数据库·人工智能·机器学习·chatgpt
IM_DALLA1 小时前
【Verilog学习日常】—牛客网刷题—Verilog快速入门—VL21
学习·fpga开发
狐心kitsune1 小时前
erlang学习:Linux常用命令1
linux·学习·erlang
乌啼霜满天2491 小时前
如何将MySQL卸载干净(win11)
数据库·mysql