MySQL的JSON操作

news/2025/1/13 3:35:40/

官网地址

1. MySQL json介绍

  • As of MySQL 5.7.8, MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents.

  • Automatic validation of JSON documents stored in JSON columns. Invalid documents produce an error.

  • Optimized storage format. JSON documents stored in JSON columns are converted to an internal format that permits quick read access to document elements.

  • It is important to keep in mind that the size of any JSON document stored in a JSON column is limited to the value of the max_allowed_packet system variable.
    在这里插入图片描述

  • A JSON column cannot have a non-NULL default value.

  • In MySQL, JSON values are written as strings. MySQL parses any string used in a context that requires a JSON value, and produces an error if it is not valid as JSON. These contexts include inserting a value into a column that has the JSON data type and passing an argument to a function that expects a JSON value (usually shown as json_doc or json_val in the documentation for MySQL JSON functions)

  • MySQL handles strings used in JSON context using the utf8mb4 character set and utf8mb4_bin collation.

  • Case sensitivity also applies to the JSON null, true, and false literals, which always must be written in lowercase.

  • In MySQL 5.7.9 and later, you can use column->path with a JSON column identifier and JSON path expression as a synonym for JSON_EXTRACT(column, path).

在这里插入图片描述

2. JSON Function Reference

在这里插入图片描述
在这里插入图片描述
MySQL 5.7.22 and later supports two aggregate JSON functions JSON_ARRAYAGG() and JSON_OBJECTAGG().
Also beginning with MySQL 5.7.22:

  • “pretty-printing” of JSON values in an easy-to-read format can be obtained using the JSON_PRETTY() function.
  • You can see how much storage space a given JSON value takes up using JSON_STORAGE_SIZE().

3. Functions That Create JSON Values

mysql> SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME());
+---------------------------------------------+
| JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME()) |
+---------------------------------------------+
| [1, "abc", null, true, "11:30:24.000000"]   |
+---------------------------------------------+mysql> SELECT JSON_OBJECT('id', 87, 'name', 'carrot');
+-----------------------------------------+
| JSON_OBJECT('id', 87, 'name', 'carrot') |
+-----------------------------------------+
| {"id": 87, "name": "carrot"}            |
+-----------------------------------------+mysql> SELECT JSON_QUOTE('null'), JSON_QUOTE('"null"');
+--------------------+----------------------+
| JSON_QUOTE('null') | JSON_QUOTE('"null"') |
+--------------------+----------------------+
| "null"             | "\"null\""           |
+--------------------+----------------------+
mysql> SELECT JSON_QUOTE('[1, 2, 3]');
+-------------------------+
| JSON_QUOTE('[1, 2, 3]') |
+-------------------------+
| "[1, 2, 3]"             |
+-------------------------+

Converting between JSON and non-JSON values

在这里插入图片描述

on duplicate key

mysql> CREATE TABLE `t_json` (->   `id` int NOT NULL AUTO_INCREMENT,->   `json_val` json DEFAULT NULL,->   PRIMARY KEY (`id`)-> ) ENGINE=InnoDB AUTO_INCREMENT=9;
mysql> select * from t_json;
Empty set (0.00 sec)mysql> insert into t_json values(1, '[123]') on duplicate key update json_val=JSON_ARRAY_APPEND(json_val, '$', 1);
Query OK, 1 row affected (0.00 sec)mysql> 
mysql> 
mysql> 
mysql> select * from t_json;
+----+----------+
| id | json_val |
+----+----------+
|  1 | [123]    |
+----+----------+
1 row in set (0.00 sec)mysql> 
mysql> insert into t_json values(1, '[123]') on duplicate key update json_val=JSON_ARRAY_APPEND(json_val, '$', 1);
Query OK, 2 rows affected (0.00 sec)mysql> select * from t_json;
+----+----------+
| id | json_val |
+----+----------+
|  1 | [123, 1] |
+----+----------+
1 row in set (0.00 sec)mysql> insert into t_json values(1, '[123]') on duplicate key update json_val=JSON_ARRAY_APPEND(json_val, '$', 1);
Query OK, 2 rows affected (0.00 sec)mysql> select * from t_json;
+----+-------------+
| id | json_val    |
+----+-------------+
|  1 | [123, 1, 1] |
+----+-------------+
1 row in set (0.01 sec)

http://www.ppmy.cn/news/990176.html

相关文章

eslint-webpack-plugin

说明:现在eslint已经弃用了eslint-loader,如果要安装来使用的话,会报错,烦死人 大概的报错信息如下: ERROR in ./src/index.js Module build failed (from ./node_modules/eslint-loader/dist/cjs.js): TypeError: Cannot read …

【雕爷学编程】MicroPython动手做(15)——掌控板之AB按键

知识点:什么是掌控板? 掌控板是一块普及STEAM创客教育、人工智能教育、机器人编程教育的开源智能硬件。它集成ESP-32高性能双核芯片,支持WiFi和蓝牙双模通信,可作为物联网节点,实现物联网应用。同时掌控板上集成了OLED…

QT中日期和时间类

QT中日期和时间类 QDateQTimeQDateTime QDate QDate类可以封装日期信息也可以通过这个类得到日期相关的信息, 包括:年, 月, 日。 // 构造函数 QDate::QDate(); QDate::QDate(int y, int m, int d);// 公共成员函数 // 重新设置日期对象中的日期 bool QDate::setDate(int year…

信创啊信创。Solon 双同时支持 jakarta.servlet 容器了!

Solon 是个神奇的项目,不是基于 Servlet 的。但是又很支持 Servlet,尤其是 war 包。打起来还挺方便的。 如果你是做信创的(听说,很多信创项目是用 war 部署到 tomcat 容器下的)。自从 javaee 改包名后,那个…

C++终止cin输入while循环时多读取^Z或^D的问题

原代码&#xff1a; istream& operator>>(istream& is, map<string, int>&mm) {string ss"";int ii0;is >> ss>>ii;mm[ss]ii;return is; }int main() {map<string,int>msi;while(cin>>msi);return 0; } 问题&…

Spring注解的原理

注解&#xff08;Annotation&#xff09;在Java中是一种元数据&#xff0c;它可以为代码提供额外的信息&#xff0c;但本身不会影响程序的执行。在Spring框架中&#xff0c;注解被广泛用于标记组件、配置依赖关系以及进行AOP等操作。我们平时是使用注解的场景有很多,原理却知之…

QGraphicsView实现简易地图1『加载离线瓦片地图』

最简单粗暴的加载方式&#xff0c;将每一层级的所有瓦片地图全部加载 注&#xff1a;该方式仅能够在瓦片地图层级较低时使用&#xff0c;否则卡顿&#xff01;&#xff01;&#xff01; 瓦片地图数据来源&#xff1a;水经注-高德地图-卫星地图 瓦片地图瓦片大小&#xff1a;25…

报数游戏、

描述 有n人围成一圈&#xff0c;顺序排号。从第1个人开始报数&#xff08;从1到3报数&#xff09;&#xff0c;凡报到3的人退出圈子&#xff0c;问最后留下的是原来的第几号的那位。。 输入 初始人数n 输出 最后一人的初始编号 输入样例 1 3 输出样例 1 2 输入样例 …