mysql supports native json instructions

text

In the beginning of MySQL 5.7.8 json native support, we will use MySQL in json type of simple instructions, I hope useful to you.

CREATE TABLE testproject (

   `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `skill` JSON NOT NULL, `student` JSON NOT NULL, PRIMARY KEY (`id`) );

View table structure:

Here Insert Picture Description

Such a field is created JSON good

Note: JSON type can not have default values.

Insert JSON

Insert json format string may be in the form of objects, or may be in the form of an array,

INSERT INTO `testproject` (student, skill) VALUES ('{"id": 1, "name": "ggjg"}', '["java", "go", "vue"]'); INSERT INTO `testproject` (student, skill) VALUES ('{"id": 5, "name": "guogege"}', '[]');

When you insert json, json database will do the check, do not meet the specification will json error.

Here Insert Picture Description

Query JSON:

Json query data in a column-> form path, wherein the object type represented $ .path this path, and the array type is $ [index]

Testproject query table student field json object id for the record of 1:

SELECT * FROM testproject WHERE student->'$.id'= 1;

Here Insert Picture Description

Query table testproject student id field of the object record json 1 or 5:

SELECT * FROM testproject WHERE student->'$.id' in (1,5); SELECT * FROM testproject WHERE student->'$.id' = 1 or student->'$.id' = 5;

Here Insert Picture Description

You can also use the function json_extract:

Here Insert Picture Description

column-> path method is limited, the data source must be a table field, otherwise error:
Here Insert Picture Description

The following query this way, check out the student -> '$ name.' Contains a double quote:

SELECT id, student->'$.id', student->'$.name', skill->'$[0]', skill->'$[2]' FROM testproject;

Here Insert Picture Description

This is not what we want, you can use double quotes JSON_UNQUOTE function will be removed, starting from MySQL 5.7.13 also by this operator - >> this and JSON_UNQUOTE are equivalent.

Here Insert Picture Description

Unlike as JSON string, so if the comparison string and JSON fields are not equal:

mysql> SELECT * FROM testproject WHERE student = '{"id": 1, "name": "ggjg"}';
Empty set (0.00 sec)

At this time, the string can be transformed into CAST as JSON:

mysql>  SELECT * FROM testproject WHERE student = CAST('{"id": 1, "name": "ggjg"}' as JSON); +----+-----------------------+---------------------------+ | id | skill | student | +----+-----------------------+---------------------------+ | 10 | ["java", "go", "vue"] | {"id": 1, "name": "ggjg"} | +----+-----------------------+---------------------------+ 1 row in set (0.01 sec)

Pay special attention to that, JSON elements in the search for a strict distinction between variable types, such as integers and strings are rigid distinctions:

mysql> SELECT * FROM testproject WHERE student->'$.id' = '1';
Empty set (0.00 sec)

mysql>
mysql> SELECT * FROM testproject WHERE student->'$.id' = 1; +----+-----------------------+---------------------------+ | id | skill | student | +----+-----------------------+---------------------------+ | 10 | ["java", "go", "vue"] | {"id": 1, "name": "ggjg"} | +----+-----------------------+---------------------------+ 1 row in set (0.00 sec)

We can see the results of the search string and integer 1 is not the same one.

Except that the above column-> path search form, can also be used JSON_CONTAINS function, but and column-> path in the form of a little contrast, JSON_CONTAINS second parameter is an integer not accepted, whether the element is an integer or a string json otherwise, this error:

mysql> SELECT * FROM testproject WHERE JSON_CONTAINS(student, 1, '$.id'); ERROR 3146 (22032): Invalid data type for JSON data in argument 2 to function json_contains; a JSON string or JSON type is required. mysql>

Here you must use the string:

mysql> SELECT * FROM testproject WHERE JSON_CONTAINS(student, '1', '$.id'); +----+-----------------------+---------------------------+ | id | skill | student | +----+-----------------------+---------------------------+ | 10 | ["java", "go", "vue"] | {"id": 1, "name": "ggjg"} | +----+-----------------------+---------------------------+ 1 row in set (0.00 sec)

JSON array type for a query, for example, contains data skill 3, JSON_CONTAINS use the same function, the same second character string parameter is also needed:

mysql> SELECT * FROM testproject WHERE JSON_CONTAINS(skill, '"go"');
+----+-----------------------+---------------------------+ | id | skill | student | +----+-----------------------+---------------------------+ | 10 | ["java", "go", "vue"] | {"id": 1, "name": "ggjg"} | +----+-----------------------+---------------------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM testproject WHERE JSON_CONTAINS(skill, '1'); +----+-----------+------------------------------+ | id | skill | student | +----+-----------+------------------------------+ | 12 | [1, 2, 3] | {"id": 4, "name": "guogege"} | +----+-----------+------------------------------+ 1 row in set (0.00 sec)

update data

MySQL does not support the form of column-> path of the update operation.

If the entire json updated, and is inserted like:

mysql> select * from testproject where id = 10; +----+-----------------------+---------------------------+ | id | skill | student | +----+-----------------------+---------------------------+ | 10 | ["java", "go", "vue"] | {"id": 1, "name": "ggjg"} | +----+-----------------------+---------------------------+ 1 row in set (0.00 sec) mysql> UPDATE testproject SET skill = '["js", "java"]' WHERE id = 10; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from testproject where id = 10; +----+----------------+---------------------------+ | id | skill | student | +----+----------------+---------------------------+ | 10 | ["js", "java"] | {"id": 1, "name": "ggjg"} | +----+----------------+---------------------------+ 1 row in set (0.00 sec)

json_array_append and json_array_insert function uses:

json_array_append json added in the back;

json_array_insert is inserted in the specified index.

mysql> select * from testproject;                                            
+----+----------------+------------------------------+                       
| id | skill          | student                      |                       
+----+----------------+------------------------------+                       
| 10 | ["js", "java"] | {"id": 1, "name": "ggjg"}    |                       
| 11 | []             | {"id": 5, "name": "guogege"} |                       
| 12 | [1, 2, 3]      | {"id": 4, "name": "guogege"} |                       
+----+----------------+------------------------------+ 3 rows in set (0.00 sec) mysql> SELECT json_array_append(skill, '$', 'c') from testproject; +------------------------------------+ | json_array_append(skill, '$', 'c') | +------------------------------------+ | ["js", "java", "c"] | | ["c"] | | [1, 2, 3, "c"] | +------------------------------------+ 3 rows in set (0.00 sec) mysql> SELECT json_array_insert(skill, '$[1]', 'php') from testproject; +-----------------------------------------+ | json_array_insert(skill, '$[1]', 'php') | +-----------------------------------------+ | ["js", "php", "java"] | | ["php"] | | [1, "php", 2, 3] | +-----------------------------------------+ 3 rows in set (0.00 sec) mysql>

json_replace, json_set, json_insert and json_remove function usage:

json_replace: only replace the old values ​​that already exist, it does not exist is ignored;

json_set: Replace the old values ​​and insert the new value does not exist;

json_insert: insert a new value, but does not replace the old value already exists;

json_remove () to remove elements.

json_replace:

mysql> select * from testproject;                                                                                      
+----+----------------+--------------------------------+                                                               
| id | skill          | student                        |                                                               
+----+----------------+--------------------------------+                                                               
| 10 | ["js", "java"] | {"id": 1, "name": "smallsoup"} |                                                               
| 11 | []             | {"id": 5, "name": "guogege"}   |                                                               
| 12 | [1, 2, 3]      | {"id": 4, "name": "guogege"}   |                                                               
+----+----------------+--------------------------------+ 3 rows in set (0.00 sec) mysql> mysql> UPDATE testproject SET student->'$.name' = 'smallsoup' where student->'$.id' = 1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio n for the right syntax to use near '->'$.name' = 'smallsoup' where student->'$.id' = 1' at line 1 mysql> mysql> UPDATE testproject SET student = json_replace(student, '$.name', 'soup') WHERE student->'$.id' = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from testproject; +----+----------------+------------------------------+ | id | skill | student | +----+----------------+------------------------------+ | 10 | ["js", "java"] | {"id": 1, "name": "soup"} | | 11 | [] | {"id": 5, "name": "guogege"} | | 12 | [1, 2, 3] | {"id": 4, "name": "guogege"} | +----+----------------+------------------------------+ 3 rows in set (0.00 sec)

json_set:

mysql> select * from testproject;
+----+----------------+------------------------------+
| id | skill          | student                      |
+----+----------------+------------------------------+
| 10 | ["js", "java"] | {"id": 1, "name": "soup"}    |
| 11 | []             | {"id": 5, "name": "guogege"} |
| 12 | [1, 2, 3]      | {"id": 4, "name": "guogege"} |
+----+----------------+------------------------------+ 3 rows in set (0.00 sec) mysql> UPDATE testproject SET student = json_set(student, '$.name', 'small', '$.age', 22) WHERE student->'$.id'= 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from testproject; +----+----------------+---------------------------------------+ | id | skill | student | +----+----------------+---------------------------------------+ | 10 | ["js", "java"] | {"id": 1, "age": 22, "name": "small"} | | 11 | [] | {"id": 5, "name": "guogege"} | | 12 | [1, 2, 3] | {"id": 4, "name": "guogege"} | +----+----------------+---------------------------------------+ 3 rows in set (0.00 sec)

json_insert:

mysql> select * from testproject;                                                                                      
+----+----------------+---------------------------------------+                                                        
| id | skill          | student                               |                                                        
+----+----------------+---------------------------------------+                                                        
| 10 | ["js", "java"] | {"id": 1, "age": 22, "name": "small"} |                                                        
| 11 | []             | {"id": 5, "name": "guogege"}          |                                                        
| 12 | [1, 2, 3]      | {"id": 4, "name": "guogege"}          |                                                        
+----+----------------+---------------------------------------+ 3 rows in set (0.00 sec) mysql> UPDATE testproject SET student = json_insert(student, '$.name', 'soup', '$.addr', '苏州') WHERE student->'$.id'= 1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from testproject; +----+----------------+---------------------------------------------------------+ | id | skill | student | +----+----------------+---------------------------------------------------------+ | 10 | ["js", "java"] | {"id": 1, "age": 22, "addr": "苏州", "name": "small"} | | 11 | [] | {"id": 5, "name": "guogege"} | | 12 | [1, 2, 3] | {"id": 4, "name": "guogege"} | +----+----------------+---------------------------------------------------------+ 3 rows in set (0.00 sec)

json_remove() :

mysql> select * from testproject;
+----+----------------+---------------------------------------------------------+
| id | skill          | student                                                 |
+----+----------------+---------------------------------------------------------+
| 10 | ["js", "java"] | {"id": 1, "age": 22, "addr": "苏州", "name": "small"}   |
| 11 | []             | {"id": 5, "name": "guogege"}                            |
| 12 | [1, 2, 3]      | {"id": 4, "name": "guogege"}                            |
+----+----------------+---------------------------------------------------------+ 3 rows in set (0.00 sec) mysql> UPDATE testproject SET student = json_remove(student, '$.name', '$.age') WHERE student->'$.id' = 1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from testproject; +----+----------------+------------------------------+ | id | skill | student | +----+----------------+------------------------------+ | 10 | ["js", "java"] | {"id": 1, "addr": "苏州"} | | 11 | [] | {"id": 5, "name": "guogege"} | | 12 | [1, 2, 3] | {"id": 4, "name": "guogege"} | +----+----------------+------------------------------+ 3 rows in set (0.00 sec)

You can see the name and age was removed.

Only listed above description of the function, mysql official list of functions provided are as follows:

Here Insert Picture Description

More usage please see the official document:

https://dev.mysql.com/doc/refman/5.7/en/json-function-reference.html

Guess you like

Origin www.cnblogs.com/EarlyBridVic/p/12154831.html