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

这篇具有很好参考价值的文章主要介绍了Postgresql中的jsonb数据类型学习使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

定义表

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
);

对应的实体模型

@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;
}

对于简单数据类型的查询

select id from tv_sup_sl_dw_query dw where dw.party_sup_other_json ->> 'childPartyBlqkbg'!='1'

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

对于复杂数据类型的查询

--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

其他一些函数的使用

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

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

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'

其他函数可以自行百度或问文心一言文章来源地址https://www.toymoban.com/news/detail-821599.html

到了这里,关于Postgresql中的jsonb数据类型学习使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Postgresql关于JSON、JSONB的操作符、处理函数(持续更新)

    (只列举本次用到的函数,其他函数在文章后面有详解) SELECT answer_id,  jsonb_array_elements(option_ids)::INTEGER  AS option_id FROM db_live_answer  WHERE survey_code=\\\'No.20230605165428002\\\'      如果所有json数组都包含两个新列的两个元素,那么使用固定路径,如dmfay的答案。 否则,你应该使用jso

    2024年02月08日
    浏览(32)
  • PostgreSQL(五)常用数据类型

    数据类型 别名 说明 character(n) char(n) 定长字符串,不足补空格 character varying(n) varchar(n) 变长字符串 text 变长字符串 数据类型 别名 说明 范围 smallint int2 有符号2字节整数 :-32768 到 +32767 integer int 、 int4 有符号4字节整数 :-2147483648 到 +2147483647 bigint int8 有符号8字节整数 :-922337203

    2024年02月06日
    浏览(33)
  • PostgreSql 数据类型

    分类名称 说明 与其他数据库的对比 布尔类型 pg支持sql标准的boolean数据类型 与mysql中的bool、boolean类型相同,占用1字节存储空间 数值类型 2字节的smallint、4字节的integer、8字节的bigint、精确类型的小数numeric、非精确的浮点小数real和double precision、自增序列类型serial,bigserial、

    2024年02月12日
    浏览(30)
  • PostgreSQL 9.6 文档: 数据类型

    目录 8.1. 数字类型 8.1.1. 整数类型 8.1.2. 任意精度数字 8.1.3. 浮点类型 8.1.4. 序数类型 8.2. 货币类型 8.3. 字符类型 8.4. 二进制数据类型 8.4.1.  bytea 的十六进制格式 8.4.2.  bytea 的逃逸格式 8.5. 日期/时间类型 8.5.1. 日期/时间输入 8.5.2. 日期/时间输出 8.5.3. 时区 8.5.4. 间隔输

    2024年02月07日
    浏览(25)
  • PostgreSQL与Java JDBC数据类型对照

     序号 数据库类型 Java类型 JDBC索引 JDBC类型 1 varchar java.lang.String 12 VARCHAR 2 char java.lang.String 1 CHAR 3 cidr java.lang.Object 1111 OTHER 4 inet java.lang.Object 1111 OTHER 5 macaddr java.lang.Object 1111 OTHER 6 text java.lang.String 12 VARCHAR 7 int8 java.lang.Long -5 BIGINT 8 bytea byte -2 BINARY 9 box java.lang.Object 1111 OTHER 1

    2024年01月16日
    浏览(37)
  • PG-DBA培训04:PostgreSQL数据类型与数据库设计规范

    一、风哥PG-DBA培训04:PostgreSQL数据类型与数据库设计规范 本课程由风哥发布的基于PostgreSQL数据库的系列课程,本课程属于PostgreSQL数据库SQL开发与应用实战阶段之PostgreSQL数据类型与数据库设计规范,学完本课程可以掌握PostgreSQL SQL语句基础讲解,PostgreSQL SQL语言基础知识,安

    2024年02月11日
    浏览(46)
  • PostgreSQL 中的 JSON:彻底改变数据库中的数据灵活性

    在这篇文章中,我们将介绍 PostgreSQL 对 JSON 对象的实现和处理方法。拥有一些 Linux、Postgres 和 JSON 方面的经验是必要的,因为我们不仅要介绍这些新功能,还要介绍如何实现它们。 本文使用在 Ubuntu 23.04 上运行的 PostgreSQL 16(开发版)编写 。首先,我将简要回顾一下 JSON 的背

    2024年01月19日
    浏览(28)
  • postgresql源码学习(55)—— 列中的NULL值是如何存储和判断的?

    问题来自 《PostgreSQL面试题集锦》学习与回答_Hehuyi_In的博客-CSDN博客 第11题        在pg元组头数据中,有一个t_bits数组,用于存储空值位图。当元组中没有null值的时候,t_bits可以被认为是空的,当元组有null值的列时,t_bits使用一个bit来表示列是否为null。 htup_details.h FLEXI

    2024年02月04日
    浏览(32)
  • Postgresql中的表结构和数据同步/数据传输到Mysql

    Postgresql中的某个表,需要连同表结构以及表数据同步一次到Mysql数据库中。 一种方式是在Postgresql中将表sql导出,然后修改sql文件语法,再导入到mysql中。 但是这种方式过于复杂,可以借助于Navicat等工具。 如果是使用Navicat进行结构同步和数据同步时,需要两边是同类型的数

    2024年02月13日
    浏览(36)
  • 【数据库】PostgreSQL中的DISTINCT ON和DISTINCT的区别

    在数据库查询中,我们经常会遇到需要去除重复数据的情况。在PostgreSQL中,我们可以使用DISTINCT和DISTINCT ON来实现这个目标。那么,它们之间有什么区别呢?本文将详细介绍这两种方法的用法、区别以及适用场景。 DISTINCT是SQL中的一个,用于从查询结果中去除重复的行

    2024年02月03日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包