当前位置:网站首页>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)
边栏推荐
- No one consults when doing research and does not communicate with students. UNC assistant professor has a two-year history of teaching struggle
- Ctfshow web entry information collection
- Bubble sort, insert sort
- [recruitment position] infrastructure software developer
- P1451 calculate the number of cells / 1329: [example 8.2] cells
- Array sorting num ranking merge in ascending order
- Usage and usage instructions of JDBC connection pool
- Brief introduction of machine learning framework
- R 熵权法计算权重及综合得分
- Bugku's steganography
猜你喜欢
Detailed explanation of C language branch statements
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
Talk about your understanding of microservices (PHP interview theory question)
基于OpenHarmony的智能金属探测器
Value series solution report
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
JS knowledge points-01
keep-alive
当代人的水焦虑:好水究竟在哪里?
sql server学习笔记
随机推荐
Bugku's eyes are not real
MySQL5.7的JSON基本操作
sql server学习笔记
Common MySQL interview questions
[recruitment position] infrastructure software developer
Bugku's Ping
queryRunner. Query method
Crud de MySQL
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
Appium自动化测试基础 — APPium基础操作API(二)
F. Weights assignment for tree edges problem solving Report
Advanced level of static and extern
Redis distributed lock principle and its implementation with PHP (2)
Appium自动化测试基础 — APPium基础操作API(一)
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
lv_ font_ Conv offline conversion
Temporary cramming before DFS examination
MySQL之CRUD
数学建模之层次分析法(含MATLAB代码)
Redis distributed lock principle and its implementation with PHP (1)