参考:http://www.lnmp.cn/mysql-57-new-features-json.html
方式一: 可以查到json中的Key:value
SELECT * FROM EDI.edi_history WHERE JSON_CONTAINS(response_summary, json_array(json_object("orderNo","10007"))) and JSON_CONTAINS(response_summary, json_array(json_object("courierCompanyCode","Air21"))) and JSON_CONTAINS(response_summary, json_array(json_object("dataSource", "EDI_AFTERSHIP"))) and JSON_CONTAINS(response_summary, json_array(json_object("courierBillNo","888888")))
方式2: 仅仅通过包含:
select * from EDI.edi_history where json_contains(response_summary, '{"dest_stn":"PRQ"}')
只更新部分值:
UPDATE EDI.edi_history SET response_summary = JSON_REPLACE(response_summary, '$.result', 'php') WHERE id = 1;
创建表
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'
);