1. 程式人生 > >MySQL 5.7的Json操作

MySQL 5.7的Json操作

info result mysql 5.7 插入記錄 res 刪除 建表 arch 1.2

創建表
CREATE TABLE t_json(id INT PRIMARY KEY, NAME VARCHAR(20) , info JSON);

插入記錄

INSERT INTO t_json(id,sname,info) VALUES(1 ,‘test‘,‘{"time":"2017-01-01 13:00:00","ip":"192.168.1.1","result":"fail"}‘);
INSERT INTO t_json(id,sname,info)  VALUES(2 ,‘my‘,JSON_OBJECT("time",NOW(),‘ip‘,‘192.168.1.1‘,‘result‘,‘fail‘));

查詢IP鍵
SELECT sname,JSON_EXTRACT(info,‘$.ip‘) FROM t_json;
查詢有多少個鍵
SELECT id,json_keys(info) AS "keys" FROM t_json;

刪除鍵
UPDATE t_json SET info = json_remove(info,‘$.ip‘);
增加鍵
UPDATE t_json SET info = json_set(info,‘$.ip‘,‘192.168.1.1‘);
變更值
UPDATE t_json SET info = json_set(info,‘$.ip‘,‘192.168.1.2‘);

MySQL 5.7的Json操作