MySQL数据类型详解:https://dev.mysql.com/doc/refman/8.0/en/json.html
MySQLJSON函数:https://dev.mysql.com/doc/refman/8.0/en/json-functions.html
数据库DDL语句:
-- auto-generated definition
create table log
(
id int auto_increment
primary key,
data text null
);
Json数组:JSON_ARRAY([val[, val] …])
# 插入json数组
insert into log (data) value (JSON_ARRAY('hot'));
insert into log (data) value (JSON_ARRAY('hot','new'));
Json对象:JSON_OBJECT([key, val[, key, val] …])
# 插入JSON对象
insert into log (data) value (JSON_OBJECT('id', 87, 'name', 'carrot'));
Json字符串:JSON_QUOTE(string)
# 插入JSON字符串
insert into log (data) value (JSON_QUOTE('null'));
insert into log(data) value (JSON_QUOTE('"null"'));
insert into log(data) value (JSON_QUOTE('[1, 2, 3]'));
JSON_CONTAINS(target, candidate[, path])
Json数组:
# 查询 data 是否包含 数值1
select * from log a where JSON_CONTAINS(a.data,'1');
select * from log a where JSON_CONTAINS(a.data,CONCAT(1));
# 查询 data 是否包含 字符串hot
select * from log a where JSON_CONTAINS(a.data,'hot'); # 报错。因为在MySQL中,去除单引号后,hot非法
select * from log a where JSON_CONTAINS(a.data,'"hot"');
select * from log a where JSON_CONTAINS(a.data,CONCAT('"','hot','"'));
Json对象:
# 查询 data里面 key为a 的 value 为1 的列
select * from log a where JSON_CONTAINS(a.data,'1','$.a');
# 查询 data里面 key为a 的 value 为{"d": 4} 的列
select * from log a where JSON_CONTAINS(a.data,'{"d": 4}','$.a');
# 查询 data里面 key为c的 value 为 {"d": 4} 的列
select * from log a where JSON_CONTAINS(a.data,'{"d": 4}','$.c');
# 查询 data里面 key为d 的 value 为 字符串5 的列
select * from log a where JSON_CONTAINS(a.data,'"5"','$.d');
select * from log a where JSON_CONTAINS(a.data,CONCAT('"','5','"'),'$.d');
JSON_EXTRACT(json_doc, path[, path] …)
-> 运算符只是简单地提取一个值。
查询 data里面 key为id 的列
# 查询 data里面 key为id 的列
select * from log a where a.data->'$.id';
select * from log a where JSON_EXTRACT(a.data,'$.id') ;
# 查询 data里面 key为id 的 value为87
select * from log a where a.data->'$.id' = 87;
select * from log a where JSON_EXTRACT(a.data,'$.id') = 87;
# 等价于:select * from log a where JSON_CONTAINS(a.data,'87','$.id');
查看date里面key为id,value为87的行,取出Json对象里面的值
# 查看date里面key为id,value为87的行,取出Json对象里面的值
select JSON_EXTRACT(data,'$.id') id
,JSON_EXTRACT(data,'$.name') name
from log
where JSON_EXTRACT(data,'$.id' )= 87;
select data->'$.id' id
,data->'$.name' name
from log
where data->'$.id' = 87;
这是一个改进的、不带引号的提取操作符。
select a.data -> '$.coverImage' as '->'
,(a.data ->> '$.coverImage') as '->>'
, concat('http://localhost:15672/',a.data -> '$.coverImage') as '->Concat'
, concat('http://localhost:15672/',a.data ->> '$.coverImage') as '->>Concat'
from log a
where a.id = 98
返回JSON文档中给定字符串的路径。
mysql json数组对象,单独提取某一个对象并提取某一个属性数据
MySQL查询小工具,替换json数组字符串中对象的属性值
搜索JSON对象数组里面字符串值
create table project_approve_info
(
id int not null
primary key,
detail_data json null,
name varchar(200) null
);
INSERT INTO test.project_approve_info (id, detail_data, name) VALUES (1, '[{"name": "技术活动名称", "value": "210"}, {"name": "计划评审时间", "value": "2021-03-20"}, {"name": "项目名称", "value": "修改编辑时的选人问题"}, {"name": "项目类别", "value": "地震勘探"}, {"name": "成果名称", "value": "成果名称"}, {"name": "评审主持人", "value": "张三"}, {"name": "工作类别", "value": "建设"}, {"name": "内容", "value": "内容"}, {"name": "表格", "value": "1"}, {"name": "费用合计", "value": "102917.1"}, {"name": "会议通知", "value": "通知"}]', '10');
INSERT INTO test.project_approve_info (id, detail_data, name) VALUES (2, '[{"name": "技术活动名称", "value": "活动1"}, {"name": "计划评审时间", "value": "2021-03-20"}, {"name": "项目名称", "value": "修改编辑时的选人问题"}, {"name": "项目类别", "value": "地震勘探"}, {"name": "成果名称", "value": "成果名称"}, {"name": "评审主持人", "value": "张三"}, {"name": "工作类别", "value": "建设"}, {"name": "内容", "value": "内容"}, {"name": "表格", "value": "1"}, {"name": "费用合计", "value": "200.1"}, {"name": "会议通知", "value": "通知"}]', '12');
SELECT
*
FROM
project_approve_info
WHERE JSON_EXTRACT( detail_data,
JSON_UNQUOTE(
REPLACE ( JSON_SEARCH( detail_data, 'all', '费用合计', NULL, '$[*].name' ), 'name', 'value' ) ) ) LIKE '%20%';
JSON_ARRAY_APPEND(json_doc, path, val[, path, val] …)
将值附加到 JSON 文档中指定数组的末尾并返回结果。
# 查看当前 99 的 data 信息
select a.data from log a where a.id = 99;
# 更改99的信息
update log a
set data = JSON_ARRAY_APPEND(a.data, '$[1]', 1)
where a.id = 99;
# 查看更改后的 99 的 data 信息
select a.data from log a where a.id = 99;