Preface Because of project needs, the storage fields are stored in JSON format. In the project, the queried values are converted into corresponding beans through Jackson for processing, which is not simple and convenient enough. MySQL has supported data in JSON format since version 5.7, which makes it very convenient to operate. Create a table When creating a new table, the field type can be directly set to json type. For example, we create a table: mysql> CREATE TABLE `test_user`(`id` INT PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, `info` JSON); JSON type fields can be NULL Insert data: mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaoming','{"sex": 1, "age": 18, "nick_name": "小萌"}'); A field of type json must be a valid json string You can use mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaohua', JSON_OBJECT("sex", 0, "age", 17)); Use mysql> INSERT INTO test_user(`name`, `info`) VALUES('xiaozhang', JSON_OBJECT("sex", 1, "age", 19, "tag", JSON_ARRAY(3,5,90))); Now view the data in the test_user table: mysql> select * from test_user; +----+-----------+--------------------------------------------+ | id | name | info | +----+-----------+--------------------------------------------+ | 1 | xiaoming | {"age": 18, "sex": 1, "nick_name": "小萌"} | | 2 | xiaohua | {"age": 17, "sex": 0} | | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90]} | +----+-----------+--------------------------------------------+ 3 rows in set (0.04 sec) Query Expression: Object is json column -> '$.key', Array is json column -> '$.key[index]' mysql> select name, info->'$.nick_name', info->'$.sex', info->'$.tag[0]' from test_user; +-----------+---------------------+---------------+------------------+ | name | info->'$.nick_name' | info->'$.sex' | info->'$.tag[0]' | +-----------+---------------------+---------------+------------------+ | xiaoming | "Xiaoming" | 1 | NULL | | xiaohua | NULL | 0 | NULL | | xiaozhang | NULL | 1 | 3 | +-----------+---------------------+---------------+------------------+ 3 rows in set (0.04 sec) Equivalent to: mysql> select name, JSON_EXTRACT(info, '$.nick_name'), JSON_EXTRACT(info, '$.sex'), JSON_EXTRACT(info, '$.tag[0]') from test_user; +-----------+----------------------------------+-----------------------------+--------------------------------+ | name | JSON_EXTRACT(info, '$.nick_name') | JSON_EXTRACT(info, '$.sex') | JSON_EXTRACT(info, '$.tag[0]') |+-----------+-----------------------------------+-----------------------------+--------------------------------+ | xiaoming | "Xiaoming" | 1 | NULL | | xiaohua | NULL | 0 | NULL | | xiaozhang | NULL | 1 | 3 | +-----------+----------------------------------+-----------------------------+--------------------------------+ 3 rows in set (0.04 sec) However, you can see that "小萌" is enclosed in double quotes, which is not what we want. You can use the JSON_UNQUOTE function to remove the double quotes. mysql> select name, JSON_UNQUOTE(info->'$.nick_name') from test_user where name='xiaoming'; +----------+-----------------------------------+ | name | JSON_UNQUOTE(info->'$.nick_name') | +----------+-----------------------------------+ | xiaoming | Xiaoming| +----------+-----------------------------------+ 1 row in set (0.05 sec) You can also use the operator ->> directly mysql> select name, info->>'$.nick_name' from test_user where name='xiaoming'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | Xiaoming| +----------+----------------------+ 1 row in set (0.06 sec) Of course, attributes can also be used as query conditions mysql> select name, info->>'$.nick_name' from test_user where info->'$.nick_name'='Xiao Meng'; +----------+----------------------+ | name | info->>'$.nick_name' | +----------+----------------------+ | xiaoming | Xiaoming| +----------+----------------------+ 1 row in set (0.05 sec) It is worth mentioning that virtual columns can be used to quickly query specified attributes of the JSON type. Create a virtual column: mysql> ALTER TABLE `test_user` ADD `nick_name` VARCHAR(50) GENERATED ALWAYS AS (info->>'$.nick_name') VIRTUAL; Note the use of the operator ->> The usage is the same as the normal column query: mysql> select name,nick_name from test_user where nick_name='小萌'; +----------+-----------+ | name | nick_name | +----------+-----------+ | xiaoming | Xiaoming| +----------+-----------+ 1 row in set (0.05 sec) renew Use mysql> UPDATE test_user SET info = JSON_INSERT(info, '$.sex', 1, '$.nick_name', '小花') where id=2; Look at the results mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 0, "nick_name": "小花"} | xiaohua| +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec) Use mysql> UPDATE test_user SET info = JSON_INSERT(info, '$.sex', 0, '$.nick_name', '小张') where id=3; Look at the results mysql> select * from test_user where id=3; +----+-----------+---------------------------------------------------------------+-----------+ | id | name | info | nick_name | +----+-----------+---------------------------------------------------------------+-----------+ | 3 | xiaozhang | {"age": 19, "sex": 1, "tag": [3, 5, 90], "nick_name": "小张"} | xiaozhang| +----+-----------+---------------------------------------------------------------+-----------+ 1 row in set (0.06 sec) Use mysql> UPDATE test_user SET info = JSON_REPLACE(info, '$.sex', 1, '$.tag', '[1,2,3]') where id=2; Look at the results mysql> select * from test_user where id=2; +----+---------+--------------------------------------------+-----------+ | id | name | info | nick_name | +----+---------+--------------------------------------------+-----------+ | 2 | xiaohua | {"age": 17, "sex": 1, "nick_name": "小花"} | xiaohua| +----+---------+--------------------------------------------+-----------+ 1 row in set (0.06 sec) You can see that the tag has not been updated. delete Removing JSON Elements Using mysql> UPDATE test_user SET info = JSON_REMOVE(info, '$.sex', '$.tag') where id=1; Look at the results mysql> select * from test_user where id=1; +----+----------+----------------------------------+-----------+ | id | name | info | nick_name | +----+----------+----------------------------------+-----------+ | 1 | xiaoming | {"age": 18, "nick_name": "Xiao Meng"} | Xiao Meng | +----+----------+----------------------------------+-----------+ 1 row in set (0.05 sec) Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. You may also be interested in:
|
<<: Introduction and usage of Angular pipeline PIPE
>>: An example of how Tomcat manages Session
Use jQuery to implement form validation, for your...
Table of contents 1. Project folder structure 1. ...
Last week, the teacher gave me a small homework, ...
Find the problem I recently migrated the storage ...
The layout of text has some formatting requiremen...
Table of contents Code cleaning "Frames"...
When we are doing front-end development, we will ...
For evenly distributed layouts, we generally use ...
Preface innodb_data_file_path is used to specify ...
Overview There are many open source network monit...
This article mainly introduces the solution to th...
Preface This article introduces how to use the ne...
Disclaimer: This password reset method can direct...
Execute the following command to report an error ...
This article shares the installation and configur...