当前位置:网站首页>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)
边栏推荐
- Severlet learning foundation
- sql server char nchar varchar和nvarchar的区别
- 可转债打新在哪里操作开户是更安全可靠的呢
- R 熵权法计算权重及综合得分
- Linear DP (basic questions have been updated)
- Ctfshow web entry command execution
- String modification problem solving Report
- Coding devsecops helps financial enterprises run out of digital acceleration
- 把 ”中台“ 的思想迁移到代码中去
- I spring and autumn blasting-2
猜你喜欢
Super wow fast row, you are worth learning!
Dark horse programmer - software testing -10 stage 2-linux and database -44-57 why learn database, description of database classification relational database, description of Navicat operation data, de
qt creater断点调试程序详解
Write a go program with vscode in one article
Ecotone technology has passed ISO27001 and iso21434 safety management system certification
Huawei Hubble incarnation hard technology IPO harvester
I include of spring and Autumn
Anti shake and throttling
P1451 求细胞数量/1329:【例8.2】细胞
Optional parameters in the for loop
随机推荐
Leetcode: Shortest Word Distance II
F. Min cost string problem solving Report
做研究无人咨询、与学生不交心,UNC助理教授两年教职挣扎史
[JVM] operation instruction
lv_font_conv离线转换
[brief notes] solve the problem of IDE golang code red and error reporting
漫画:程序员不是修电脑的!
MySQL表字段调整
Leetcode: Shortest Word Distance II
Huiyuan, 30, is going to have a new owner
DVWA range clearance tutorial
Misc Basic test method and knowledge points of CTF
MySQL----函数
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
SQL Server learning notes
Ten billion massage machine blue ocean, difficult to be a giant
F. Weights assignment for tree edges problem solving Report
Detailed explanation of C language branch statements
I spring and autumn blasting-2
12 MySQL interview questions that you must chew through to enter Alibaba