当前位置:网站首页>Basic JSON operations of MySQL 5.7
Basic JSON operations of MySQL 5.7
2022-07-05 15:29:00 【Someone Yang believed your evil】
MySQL5.7 Of JSON Basic operation
1、 Build table
When creating a new table, the field type can be directly set to json type , For example, we create a table :
create table `test_user`
(
`id` int primary key auto_increment,
`name` varchar(50) not null,
`info` json
);
json The type field can be NULL
2、 Insert
(1) Build directly with strings json object
insert into test_user(`name`, `info`)
values ('xiaoming', '{ "sex": 1, "age": 18, "nick_name": " Xiaomeng " }');
json A field of type must be a valid json character string
(2) have access to JSON_OBJECT() The function structure json object
insert into test_user(`name`, `info`)
values ('xiaohua', json_object("sex", 0, "age", 17));
(3) Use JSON_ARRAY() The function structure json Array
insert into test_user(`name`, `info`)
values ('xiaozhang', json_object("sex", 1, "age", 19, "tag", json_array(3, 5, 90)));
see test_user Table data :
mysql> select * from test_user;
+----+-----------+----------------------------------------------+
| id | name | info |
+----+-----------+----------------------------------------------+
| 1 | xiaoming | {
"age": 18, "sex": 1, "nick_name": " Xiaomeng "} |
| 2 | xiaohua | {
"age": 17, "sex": 0} |
| 3 | xiaozhang | {
"age": 19, "sex": 1, "tag": [3, 5, 90]} |
+----+-----------+----------------------------------------------+
3 rows in set (0.00 sec)
3、 Inquire about
(1) Inquire about json attribute
expression : The object is json Column ->'$. key ', The array is json Column ->'$. key [index]'
mysql> select name, info -> '$.nick_name' as nick_name, info -> '$.sex' as sex, info -> '$.tag[0]' as 1st_tag from test_user;
+-----------+-----------+------+---------+
| name | nick_name | sex | 1st_tag |
+-----------+-----------+------+---------+
| xiaoming | " Xiaomeng " | 1 | NULL |
| xiaohua | NULL | 0 | NULL |
| xiaozhang | NULL | 1 | 3 |
+-----------+-----------+------+---------+
3 rows in set (0.00 sec)
Equivalent to : The object is JSON_EXTRACT(json Column , '$. key '), The array is JSON_EXTRACT(json Column , '$. key [index]')
mysql> select name, JSON_EXTRACT(info, '$.nick_name') as nick_name, JSON_EXTRACT(info, '$.sex') as sex, JSON_EXTRACT(info, '$.tag[0]') as 1st_tag from test_user;
+-----------+-----------+------+---------+
| name | nick_name | sex | 1st_tag |
+-----------+-----------+------+---------+
| xiaoming | " Xiaomeng " | 1 | NULL |
| xiaohua | NULL | 0 | NULL |
| xiaozhang | NULL | 1 | 3 |
+-----------+-----------+------+---------+
3 rows in set (0.00 sec)
But when you see it " Xiaomeng " It's in double quotation marks , This is not what we want , It can be used JSON_UNQUOTE Function to remove double quotes
mysql> select name, JSON_UNQUOTE(info -> '$.nick_name') as nick_name from test_user where name = 'xiaoming';
+----------+-----------+
| name | nick_name |
+----------+-----------+
| xiaoming | Xiaomeng |
+----------+-----------+
1 row in set (0.00 sec)
You can also use the operator directly ->>
mysql> select name, info ->> '$.nick_name' as nick_name from test_user where name = 'xiaoming';
+----------+-----------+
| name | nick_name |
+----------+-----------+
| xiaoming | Xiaomeng |
+----------+-----------+
1 row in set (0.00 sec)
(2)json Attribute as query criteria
mysql> select name, info ->> '$.nick_name' as nick_name from test_user where info -> '$.nick_name' = ' Xiaomeng ';
+----------+-----------+
| name | nick_name |
+----------+-----------+
| xiaoming | Xiaomeng |
+----------+-----------+
1 row in set (0.00 sec)
(3) adopt Virtual column Yes JSON Quick query of the specified properties of type .
Create virtual columns :
mysql> alter table `test_user` add `nick_name` varchar(50) generated always as (info ->> '$.nick_name') virtual;
Note the operator ->> Remove double quotes
Delete virtual column
mysql> alter table `test_user` drop `nick_name`;
When used, it is the same as the ordinary type of column query :
mysql> select name, nick_name from test_user where nick_name = ' Xiaomeng ';
+----------+-----------+
| name | nick_name |
+----------+-----------+
| xiaoming | Xiaomeng |
+----------+-----------+
1 row in set (0.00 sec)
4、 to update
(1) Use JSON_INSERT() Insert new value , But it will not overwrite the existing value
Before updating
mysql> select * from test_user where id = 2;
+----+---------+-----------------------+-----------+
| id | name | info | nick_name |
+----+---------+-----------------------+-----------+
| 2 | xiaohua | {
"age": 17, "sex": 0} | NULL |
+----+---------+-----------------------+-----------+
1 row in set (0.00 sec)
to update
mysql> update test_user set info = json_insert(info, '$.sex', 1, '$.nick_name', ' floret ') where id = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
After the update
mysql> select * from test_user where id = 2;
+----+---------+----------------------------------------------+-----------+
| id | name | info | nick_name |
+----+---------+----------------------------------------------+-----------+
| 2 | xiaohua | {
"age": 17, "sex": 0, "nick_name": " floret "} | floret |
+----+---------+----------------------------------------------+-----------+
1 row in set (0.00 sec)
(2) Use JSON_SET() Insert new value , And overwrite the existing value
Before updating
mysql> select * from test_user where id = 3;
+----+-----------+------------------------------------------+-----------+
| id | name | info | nick_name |
+----+-----------+------------------------------------------+-----------+
| 3 | xiaozhang | {
"age": 19, "sex": 1, "tag": [3, 5, 90]} | NULL |
+----+-----------+------------------------------------------+-----------+
1 row in set (0.00 sec)
to update
mysql> update test_user set info = json_set(info, '$.age', 20, '$.sex', 0, '$.nick_name', ' Xiao Zhang ') where id = 3;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
After the update
mysql> select * from test_user where id = 3;
+----+-----------+-----------------------------------------------------------------+-----------+
| id | name | info | nick_name |
+----+-----------+-----------------------------------------------------------------+-----------+
| 3 | xiaozhang | {
"age": 20, "sex": 0, "tag": [3, 5, 90], "nick_name": " Xiao Zhang "} | Xiao Zhang |
+----+-----------+-----------------------------------------------------------------+-----------+
1 row in set (0.00 sec)
(3) Use JSON_REPLACE() Replace only existing values
Before updating
mysql> select * from test_user where id = 2;
+----+---------+----------------------------------------------+-----------+
| id | name | info | nick_name |
+----+---------+----------------------------------------------+-----------+
| 2 | xiaohua | {
"age": 17, "sex": 0, "nick_name": " floret "} | floret |
+----+---------+----------------------------------------------+-----------+
1 row in set (0.00 sec)
to update
mysql> update test_user set info = json_replace(info, '$.sex', 1, '$.tag', '[1,2,3]') where id = 2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
After the update
mysql> select * from test_user where id = 2;
+----+---------+----------------------------------------------+-----------+
| id | name | info | nick_name |
+----+---------+----------------------------------------------+-----------+
| 2 | xiaohua | {
"age": 17, "sex": 1, "nick_name": " floret "} | floret |
+----+---------+----------------------------------------------+-----------+
1 row in set (0.00 sec)
You can see tag Not updated
(4) Use JSON_REMOVE() Delete JSON Elements
Before updating
mysql> select * from test_user where id = 1;
+----+----------+----------------------------------------------+-----------+
| id | name | info | nick_name |
+----+----------+----------------------------------------------+-----------+
| 1 | xiaoming | {
"age": 18, "sex": 1, "nick_name": " Xiaomeng "} | Xiaomeng |
+----+----------+----------------------------------------------+-----------+
1 row in set (0.00 sec)
to update
mysql> update test_user set info = json_remove(info, '$.sex', '$.tag') where id = 1;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
After the update
mysql> select * from test_user where id = 1;
+----+----------+------------------------------------+-----------+
| id | name | info | nick_name |
+----+----------+------------------------------------+-----------+
| 1 | xiaoming | {
"age": 18, "nick_name": " Xiaomeng "} | Xiaomeng |
+----+----------+------------------------------------+-----------+
1 row in set (0.00 sec)
边栏推荐
- Ten billion massage machine blue ocean, difficult to be a giant
- Advanced level of static and extern
- Write a go program with vscode in one article
- swiper. JS to achieve barrage effect
- Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
- 亿咖通科技通过ISO27001与ISO21434安全管理体系认证
- String modification problem solving Report
- Common PHP interview questions (1) (written PHP interview questions)
- Object. defineProperty() - VS - new Proxy()
- 1330: [example 8.3] minimum steps
猜你喜欢

Ionic Cordova project modification plug-in

Creation and use of thymeleaf template

Crud de MySQL

当代人的水焦虑:好水究竟在哪里?

数据库学习——数据库安全性

Detailed explanation of C language branch statements

Appium自动化测试基础 — APPium基础操作API(一)

Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)

【簡記】解决IDE golang 代碼飄紅報錯

基于OpenHarmony的智能金属探测器
随机推荐
Bugku's Eval
Ctfshow web entry command execution
CSDN I'm coming
Calculate weight and comprehensive score by R entropy weight method
P1451 calculate the number of cells / 1329: [example 8.2] cells
MySQL之CRUD
Cartoon: programmers don't repair computers!
Redis distributed lock principle and its implementation with PHP (2)
OSI 七层模型
Hongmeng system -- Analysis from the perspective of business
Ionic Cordova project modification plug-in
Maximum common subsequence
Number protection AXB function! (essence)
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
episodic和batch的定义
ICML 2022 | 探索语言模型的最佳架构和训练方法
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
MySQL 巨坑:update 更新慎用影响行数做判断!!!
Value series solution report
华为哈勃化身硬科技IPO收割机