/*DROP TABLE IF EXISTS `sync_test2`;
CREATE TABLE `sync_test2` (
`id` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '主键id',
`product_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品ID',
`test_json` json,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '服务商品服务价格' ROW_FORMAT = Dynamic;
INSERT INTO `sync_test2` VALUES (2, 'tttq', '{\"pin\": \"560103\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (3, '10011', '{\"pin\": \"10011\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (4, '10012', '{\"pin\": \"10012\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (5, '10013', '{\"pin\": \"10013\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (6, '10014', '{\"pin\": \"10014\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (7, '10015', '{\"pin\": \"10015\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (8, '10016', '{\"pin\": \"10016\", \"email\": \"[email protected]\"}');
INSERT INTO `sync_test2` VALUES (9, '10016', '{\"pin\": \"10016\", \"user\":{\"name\": \"560103\"}, \"email\": \"[email protected]\"}');
*/
/*
分类 函数 描述
创建json
json_array 创建json数组
json_object 创建json对象
json_quote 将json转成json字符串类型
查询json
json_contains 判断是否包含某个json值
json_contains_path 判断某个路径下是否包json值
json_extract 提取json值
column->path json_extract的简洁写法,MySQL 5.7.9开始支持
column->>path json_unquote(column -> path)的简洁写法
json_keys 提取json中的键值为json数组
json_search 按给定字符串关键字搜索json,返回匹配的路径
修改json
json_append 废弃,MySQL 5.7.9开始改名为json_array_append
json_array_append 末尾添加数组元素,如果原有值是数值或json对 象,则转成数组后,再添加元素
json_array_insert 插入数组元素
json_insert 插入值(插入新值,但不替换已经存在的旧值)
json_merge 合并json数组或对象
json_remove 删除json数据
json_replace 替换值(只替换已经存在的旧值)
json_set 设置值(替换旧值,并插入不存在的新值)
json_unquote 去除json字符串的引号,将值转成string类型
返回json属性
json_depth 返回json文档的最大深度
json_length 返回json文档的长度
json_type 返回json值得类型
json_valid 判断是否为合法json文档
*/
#根据条件查询
select * from sync_test2 where test_json ->> '$.user.name' = 'wcj';
select * from sync_test2 where test_json ->> '$.email' like '10011%';
select product_id,test_json ->> '$.pin' pin from sync_test2 where test_json ->> '$.email' like '10011%';
select id,product_id,json_pretty(test_json ->> '$.user')pin from sync_test2 where test_json ->> '$.pin' = '10016';
#查询json中值存不存在
select json_contains(test_json ->> '$.pin',"560103") from sync_test2;
#查询json中key存不存在
select json_contains_path(test_json, 'one', "$.user.name") from sync_test2;
select json_contains_path(test_json, 'all', "$.user", "$.email") from sync_test2;
#查询json中所有key
select json_keys(test_json) from sync_test2 where test_json ->> '$.user.name' = 'wcj';
#值变更
#替换现有值并添加不存在的值
update sync_test2 set test_json = json_set(test_json, "$.user.name", "wcj") where id = 9;
#插入值,但不替换现有值
update sync_test2 set test_json = json_insert(test_json, "$.user.age", 30) where id = 9;
#仅替换现有值
update sync_test2 set test_json = json_replace(test_json, "$.user.age", 40, "$.user.grade", 100) where id = 9;
#能从JSON文档中删除数据
update sync_test2 set test_json = json_remove(test_json, "$.user.age") where id = 9;
#===================JSON基础工具======================
#使用JSON_ARRAY方法定义JSON数组;
SELECT JSON_ARRAY(1, "abc", NULL, TRUE, CURTIME())
#结果:[1, "abc", null, true, "11:30:24.000000"]
#JSON_OBJECT 方法定义JSON对象
SELECT JSON_OBJECT('id', 87, 'name', 'carrot')
#结果{"id": 87, "name": "carrot"}
#JSON_QUOTE 将JSON对象转义成String, 就是将内部的符 号进行转义,并整体包裹上双引号;
SELECT JSON_QUOTE(' "null" ')
#结果 "\"null\""
#将JSON内容美化并输出;
select JSON_PRETTY(test_json) from sync_test2 where id = 9;
#类似 ->的用法不同的是,返回的是里面值的数组
select JSON_EXTRACT(test_json, '$.user.*') from sync_test2 where id = 9;
select JSON_EXTRACT(test_json, '$[1 to 2]') from sync_test2 where id = 10;
# 获取数据一级目录的值
SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.*');
#[1, 2, [3, 4, 5]]
#获取二级目录里面所有的值
SELECT JSON_EXTRACT('{"a": 1, "b": 2, "c": [3, 4, 5]}', '$.c[*]')
#[3, 4, 5]
# 获取数据中所有key为b的值
SELECT JSON_EXTRACT('{"a": {"b": 1}, "c": {"b": 2},"b":9}', '$**.b');
#[1, 2]
#获取数组中1-3位中的值
SELECT JSON_EXTRACT('[1, 2, 3, 4, 5]', '$[1 to 3]');
#[2, 3, 4]
#把json数据转成表
SELECT * FROM JSON_TABLE('[{"a":"3"},{"a":2},{"b":1},{"a":0},{"a":[1,2]}]'
,"$[*]" COLUMNS(rowid FOR ORDINALITY
,ac VARCHAR(100) PATH "$.a" DEFAULT '111' ON EMPTY DEFAULT '999' ON ERROR
,aj JSON PATH "$.a" DEFAULT '{"x": 333}' ON EMPTY
,bx INT EXISTS PATH "$.b")
) AS tt;
-- 追加成数组,IFNULL(json_test,'{}' )为字段null赋值“{}”值
update t_user set json_test = JSON_MERGE(IFNULL(json_test,'{}' ),'{"name":"test1"}') where id = 1;
-- json字段设置值
update t_user set json_test = JSON_SET(IFNULL(json_test,'{}'),'$.name','test11','$.type',2) where id = 1;
--移除json字段中keyd的值
update t_user set json_test = json_remove(json_test,'$.name','$.type') where id = 1;