当前位置:网站首页>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)
边栏推荐
- Definition of episodic and batch
- Bugku telnet
- qt creater断点调试程序详解
- wxml2canvas
- Lesson 4 knowledge summary
- Creation and use of thymeleaf template
- Common interview questions about swoole
- How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
- 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
- Where is the operation of convertible bond renewal? Is it safer and more reliable to open an account
猜你喜欢

Bugku's Eval
![[brief notes] solve the problem of IDE golang code red and error reporting](/img/b6/0b2ea06eb5fbe651ff9247b109fa15.png)
[brief notes] solve the problem of IDE golang code red and error reporting

OSI 七层模型

一文搞定vscode编写go程序

"Sequelae" of the withdrawal of community group purchase from the city

Number protection AXB function! (essence)

基于OpenHarmony的智能金属探测器

Value series solution report

Common redis data types and application scenarios

wxml2canvas
随机推荐
mapper.xml文件中的注释
Bugku's Ping
Definition of episodic and batch
Go learning ----- relevant knowledge of JWT
Object. defineProperty() - VS - new Proxy()
Fr exercise topic --- comprehensive question
P1451 calculate the number of cells / 1329: [example 8.2] cells
Brief introduction of machine learning framework
First PR notes
Fr exercise topic - simple question
Leetcode: Shortest Word Distance II
Value series solution report
[recruitment position] Software Engineer (full stack) - public safety direction
当代人的水焦虑:好水究竟在哪里?
Garbage collection mechanism of PHP (theoretical questions of PHP interview)
Can gbase 8A view the location of SQL statement history?
如何将 DevSecOps 引入企业?
Bugku's steganography
B站做短视频,学抖音死,学YouTube生?
Common MySQL interview questions (1) (written MySQL interview questions)