当前位置:网站首页>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)
边栏推荐
- 一文搞定vscode编写go程序
- Summary of the second lesson
- I spring and autumn blasting-1
- Detailed explanation of C language branch statements
- Crud de MySQL
- B站做短视频,学抖音死,学YouTube生?
- 超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
- Bugku's steganography
- Anaconda uses China University of science and technology source
- Cartoon: programmers don't repair computers!
猜你喜欢
随机推荐
Bugku cyberpunk
Transfer the idea of "Zhongtai" to the code
1330:【例8.3】最少步数
wxml2canvas
Bugku's Ping
Bugku's Eval
Leetcode: Shortest Word Distance II
Ctfshow web entry explosion
swiper. JS to achieve barrage effect
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
Definition of episodic and batch
Common MySQL interview questions
The difference between SQL Server char nchar varchar and nvarchar
Creation and use of thymeleaf template
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
ICML 2022 | explore the best architecture and training method of language model
Aike AI frontier promotion (7.5)
Hongmeng system -- Analysis from the perspective of business
Anti shake and throttling
Super wow fast row, you are worth learning!









