hopeless-dream

导航

MySQL json应用

json基础应用

CREATE TABLE test_json(id INT PRIMARY KEY ,u_name VARCHAR(20),info json);

insert插入json记录

插入json数组

INSERT INTO test_json VALUES(1,'tom',json_array(99,'测试',NULL,TRUE,CURTIME()));

mysql> SELECT * FROM test_json;
+----+--------+-----------------------------------------------+
| id | u_name | info                                          |
+----+--------+-----------------------------------------------+
|  1 | tom    | [99, "测试", null, true, "23:03:35.000000"]   |
+----+--------+-----------------------------------------------+
1 row in set (0.00 sec)

插入json对象(自动转换成k-v形式)

INSERT INTO test_json VALUES(2,'jerry',json_object('age',15,'time',NOW()));

mysql> SELECT * FROM test_json;
+----+--------+---------------------------------------------------+
| id | u_name | info                                              |
+----+--------+---------------------------------------------------+
|  1 | tom    | [99, "测试", null, true, "23:03:35.000000"]       |
|  2 | jerry  | {"age": 15, "time": "2020-06-19 23:07:24.000000"} |
+----+--------+---------------------------------------------------+
2 rows in set (0.00 sec)
INSERT INTO test_json VALUES(3,'jerry','{"city":"北京市","`time`":"2020-06-19 23:07:28"}');

mysql> SELECT * FROM test_json;
+----+--------+--------------------------------------------------------+
| id | u_name | info                                                   |
+----+--------+--------------------------------------------------------+
|  1 | tom    | [99, "测试", null, true, "23:03:35.000000"]            |
|  2 | jerry  | {"age": 15, "time": "2020-06-19 23:07:24.000000"}      |
|  3 | jerry  | {"city": "北京市", "`time`": "2020-06-19 23:07:28"}    |
+----+--------+--------------------------------------------------------+

select查询json记录

mysql> SELECT json_extract(info,'$.age') FROM test_json;
+----------------------------+
| json_extract(info,'$.age') |
+----------------------------+
| NULL                       |
| 15                         |
| NULL                       |
+----------------------------+
3 rows in set (0.00 sec)

mysql> SELECT json_extract(info,'$.age','$.city') FROM test_json;
+-------------------------------------+
| json_extract(info,'$.age','$.city') |
+-------------------------------------+
| NULL                                |
| [15]                                |
| ["北京市"]                          |
+-------------------------------------+
3 rows in set (0.00 sec)

查询key

mysql> select id,json_keys(info) from test_json;
+----+--------------------+
| id | json_keys(info)    |
+----+--------------------+
|  1 | NULL               |
|  2 | ["age", "time"]    |
|  3 | ["city", "`time`"] |
+----+--------------------+
3 rows in set (0.00 sec)

update 修改记录

增加键

UPDATE test_json SET info = json_set(info,'$.ip','10.0.0.10') WHERE id =3;
mysql> SELECT * FROM test_json;
+----+--------+---------------------------------------------------------------------------+
| id | u_name | info                                                                      |
+----+--------+---------------------------------------------------------------------------+
|  1 | tom    | [99, "测试", null, true, "23:03:35.000000"]                               |
|  2 | jerry  | {"age": 15, "time": "2020-06-19 23:07:24.000000"}                         |
|  3 | jerry  | {"ip": "10.0.0.10", "city": "北京市", "`time`": "2020-06-19 23:07:28"}    |
+----+--------+---------------------------------------------------------------------------+
3 rows in set (0.00 sec)

删除键

mysql> UPDATE test_json SET info = json_remove(info,'$.ip') WHERE id = 3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM test_json;
+----+--------+--------------------------------------------------------+
| id | u_name | info                                                   |
+----+--------+--------------------------------------------------------+
|  1 | tom    | [99, "测试", null, true, "23:03:35.000000"]            |
|  2 | jerry  | {"age": 15, "time": "2020-06-19 23:07:24.000000"}      |
|  3 | jerry  | {"city": "北京市", "`time`": "2020-06-19 23:07:28"}    |
+----+--------+--------------------------------------------------------+
3 rows in set (0.00 sec)

 

posted on 2020-06-20 21:05  hopeless-dream  阅读(189)  评论(0编辑  收藏  举报