示例表
后面的所有的表demo_json結(jié)構(gòu)都是這個(gè)
create table demo_json(
`id` bigint unsigned not null auto_increment,
`json` json not null,
primary key (`id`)
)engine=innodb;
json_set
用于將對(duì)應(yīng)的json已有的字段進(jìn)行修改
語法
json_set(json_doc, path, value, [path, value, ...])
說明:
其中json_doc就是表中對(duì)應(yīng)的json列竟趾,path就是json中對(duì)應(yīng)的字段key夷陋,value就是對(duì)應(yīng)的值,后面的都是這樣。返回值就是修改后的值
-- 插入數(shù)據(jù)
insert into demo_json(`json`) values ('{"ok":12}');
-- 更新數(shù)據(jù)
update demo_json set `json`=json_set(`json`, '$.f1', 2333, '$.f2', "v1");
-- 選擇數(shù)據(jù)
select `json` from demo_json;
-- 返回
{"f1": 2333, "f2": "v1", "ok": 12}
json_keys
返回對(duì)應(yīng)文檔中的最上層的keys钦购,如果內(nèi)部還有更多嵌套的key,則是不會(huì)嵌套返回的
json_keys(json_doc[, path])
-- 無path參數(shù)互亮,返回的是json_doc中的頂級(jí)key
-- 返回[a,b]
select json_kesy('{"a":12, "b":32}');
-- 返回[a, b, c]
select json_keys('{"a":12, "b":32, "c":{"ok":11, "kk":43}}');
-- 有path參數(shù)汉操,則返回的是path對(duì)應(yīng)文檔中的頂級(jí)key
-- 返回[ok, kk]
select json_keys('{"a":12, "b":32, "c":{"ok":11, "kk":43}}', '$.c');
json_type
返回對(duì)應(yīng)文檔中的字段值的類型
json_type(value)
普通情況下直接返回,但是如果文檔的話凿菩,可以用函數(shù)json_extract(json_doc, path[, path...]) 進(jìn)行提取即可
-- 返回integer
select json_type('12');
-- 返回double
select json_type('12.0');
-- 返回string
select json_type('"abc"');
-- 返回object
select json_type('{"a":12,"b":"v1"}');
-- 返回array
select json_type('["a", 1]');
-- 返回 {"a": 12, "b": "vv"}
select `json` from demo_json where id = 3;
-- 返回integer
select json_type(json_extract(`json`, '$.a')) from demo_json where id = 3;
-- 返回string
select json_type(json_extract(`json`, '$.b')) from demo_json where id = 3;
json_array
該函數(shù)用于將數(shù)據(jù)進(jìn)行拼接机杜,其實(shí)就有點(diǎn)像java中的new ArrayList() 這種
json_array(value[,value...])
-- 返回?cái)?shù)組:["a", "1", 34]
select json_array('a', '1', 34);
json_depth
返回文檔的深度
json_depth(json_doc)
-- 返回1
select json_depth('[]');
-- 返回1
select json_depth('{}');
-- 返回1
select json_depth('12');
-- 返回1
select json_depth('"a"');
-- 返回2
select json_depth('[1]');
-- 返回2
select json_depth('[1, 2, 3, "a"]');
-- 返回2
select json_depth('{"a":12}');
-- 返回2
select json_depth('{"a":12, "b":"v"}');
-- 返回3
select json_depth('["a", {"b":12}]');
-- 返回3
select json_depth('[{"a":10}, {"b":12}]');
-- 返回3
select json_depth('{"a":12, "b":{"b1":12}}');
其中普通的空以及普通字段,深度是1級(jí)衅谷,二級(jí)的話椒拗,就是普通的數(shù)組和對(duì)象
json_quote
將非json_doc文檔格式的數(shù)據(jù),轉(zhuǎn)換為文檔格式
json_quote(string)
-- 返回 ""
select json_quote('a');
-- 返回 "\"a\""
select json_quote('"a"');
-- 返回 ""
select json_quote('');
-- 返回 "[a, b]"
select json_quote('[a, b]');
-- 返回 "[\"a\", \"b\"]"
select json_quote('["a", "b"]');
json_valid
判斷值是否是json類型
json_valid(val)
-- 返回 null
select json_valid(null);
-- 返回 0
select json_valid('');
-- 返回 0
select json_valid('a');
-- 返回 1
select json_valid('[1,2]');
-- 返回 0
select json_valid('{a,1}');
-- 返回 1
select json_valid('{"a":12, "b":2}');
json_insert
給對(duì)應(yīng)的文檔添加數(shù)據(jù)获黔,這個(gè)給update的時(shí)候蚀苛,這樣設(shè)置,更方便
select json_insert(json_doc, path, val[, path, val] ...)
-- 插入數(shù)據(jù)
insert into demo_json(`json`) values ('{"a":1, "b":2}');
-- {"a": 1, "b": 2}
select `json` from demo_json;
update demo_json set `json`=json_insert(`json`, '$.c', '3');
-- {"a": 1, "b": 2, "c": "3"}
select `json` from demo_json;
json_length
返回對(duì)應(yīng)文檔的長度玷氏,我們知道文檔有這么幾種類型:標(biāo)量堵未、對(duì)象、數(shù)組
文件長度確定如下:
標(biāo)量的長度為1盏触。
數(shù)組的長度是數(shù)組元素的數(shù)量渗蟹。
對(duì)象的長度是對(duì)象成員的數(shù)量。
該長度不計(jì)算嵌套數(shù)組或?qū)ο蟮拈L度赞辩。
-- 返回錯(cuò)誤
select json_length('a');
select json_length(1);
select json_length('');
-- 0
select json_length('{}');
select json_length('[]');
select json_length('null');
-- 1
select json_length('"2"');
select json_length('[1]');
select json_length('{"a":1}');
-- 2
select json_length('[1, "a"]');
select json_length('{"a":1, "b":2}');
--------- 文檔類型 ---------
truncate demo_json;
insert into demo_json(`json`) values ('{"a": 1, "b": 2, "c": {"c1":1, "c2": 2}}');
-- {"a": 1, "b": 2, "c": {"c1":1, "c2": 2}}
select `json` from demo_json;
-- 1
select json_length(`json`, '$.a') from demo_json where id = 1;
-- 2
select json_length(`json`, '$.c') from demo_json where id = 1;
-- 3
select json_length(`json`) from demo_json where id = 1;
json_object
其實(shí)就是把一些值轉(zhuǎn)換為object格式雌芽,跟函數(shù)json_array有點(diǎn)相同
json_object([key, val[, key, val] ...])
json_pretty
該函數(shù)就是把文檔給打印出來,按照json格式進(jìn)行打印
-- 1
select json_pretty('1');
-- "a"
select json_pretty('"a"');
-- 返回
-- [
-- 1,
-- 2,
-- "a"
-- ]
select json_pretty('[1, 2, "a"]');
-- 返回
-- {
-- "a": 1,
-- "b": 12,
-- "c": 39
-- }
select json_pretty('{"a":1, "b":12, "c":39}');
json_remove
從文檔中刪除指定的元素辨嗽,然后返回
select json_remove(json_doc, path[, path] ...)
-- {"b": 2, "c": 3}
select json_remove('{"a":1, "b":2, "c":3}', '$.a');
-- {"c": 3}
select json_remove('{"a":1, "b":2, "c":3}', '$.a', '$.b');
-- [3, 2]
select json_remove('[12, 3, 2]', '$[0]');
-- [12, 2]
select json_remove('[12, 3, 2]', '$[1]');
------- 使用在字段上 ------
truncate demo_json;
insert into demo_json(`json`) values ('{"a":12, "b":2}');
update demo_json set `json`=json_remove(`json`, '$.a') where id = 1;
-- {"b": 2}
select `json` from demo_json;
json_search
該函數(shù)返回的是指定字符串的路徑世落,就是doc中的字段
json_search(json_doc, one_or_all, search_str[, escape_char[, path] ...])
說明:
- one_or_all:
'one':搜索到一個(gè)就直接返回
'all':搜索到所有的才返回,所有的字段會(huì)包裝成一個(gè)數(shù)組
- search_str:這個(gè)是搜索字段糟需,默認(rèn)是全部匹配屉佳,可以模糊匹配谷朝,采用%和,%表示匹配多個(gè)武花,表示匹配一個(gè)字符圆凰,這個(gè)跟like使用方式是一樣的
- escape_char:這個(gè)值轉(zhuǎn)義符,如果搜索的字符中有需要轉(zhuǎn)義的髓堪,則請(qǐng)?jiān)谠撟址@了添加送朱,默認(rèn)是\,通常情況下請(qǐng)?zhí)顚憺榭栈蛘遪ull干旁,必須要有一個(gè)值
- path:更多的指定的字段
注意:該命令只是用于搜索字符使用
-- $.a
select json_search('{"a":"v"}', 'all', "v");
-- $.b
select json_search('{"a":"v", "b":"women is ok"}', 'all', "%is%");
-- $.a
select json_search('{"a":"v", "b":"women is ok", "c":{"c1":"v", "c2":"v2"}}', 'one', "v");
-- ["$.a", "$.c.c1"]
select json_search('{"a":"v", "b":"women is ok", "c":{"c1":"v", "c2":"v2"}}', 'all', "v");
-- ["$.a", "$.c.c1", "$.c.c2"]
select json_search('{"a":"v", "b":"women is ok", "c":{"c1":"v", "c2":"v2"}}', 'all', "%v%");
-- ["$.c.c1", "$.c.c2"]
select json_search('{"a":"v", "b":"women is ok", "c":{"c1":"v", "c2":"v2"}}', 'all', "%v%", null, '$.c');
-- 注意:數(shù)字搜索全部返回null驶沼,這個(gè)只是搜索字符的
select json_search('{"a":2}', 'all', 2);
select json_search('{"a":2}', 'all', '2');
select json_search('{"a":2}', 'all', '"2"');
select json_search('{"a":2}', 'all', '%2');
select json_search('{"a":2}', 'all', '%2%');
json_extract
該命令是從doc文件中提取對(duì)應(yīng)的值
select json_extract(json_doc, path[, path] ...)
-- 1
select json_extract('{"a":1, "b":2}', '$.a');
-- [1, 2]
select json_extract('{"a":1, "b":2}', '$.a', '$.b');
-- [1, 2, {"c1": "v1", "c2": "v2"}]
select json_extract('{"a":1, "b":2, "c":{"c1":"v1", "c2":"v2"}}', '$.a', '$.b', '$.c');
json_unquote
該函數(shù)用于去除轉(zhuǎn)義符,和函數(shù)json_quote是作用相反
json_quote(string)
-- "\"123\""
select json_quote('"123"');
-- "123"
select json_quote('123');
-- 123
select json_unquote('123');
-- 123
select json_unquote('"123"');
json_contains
判斷一個(gè)文檔內(nèi)容是否包含另外一個(gè)內(nèi)容
json_contains(target, candidate[, path])
-- --- 對(duì)象包含:只有全部包含才返回1
-- 1
select json_contains('{"a":12}', '{"a":12}');
-- 0
select json_contains('{"a":1}', '{"a":12}');
-- 1
select json_contains('{"a":12, "b":2}', '{"a":12}');
-- --- 數(shù)組包含:只有全部包含才返回1
-- 1
select json_contains('[1, 2, "a"]', '1');
-- 1
select json_contains('[1, 2, "a"]', '"a"');
-- 1
select json_contains('[1, 2, "a"]', '[1, 2]');
-- 0
select json_contains('[1, 2, "a"]', '[1, 2, "b"]');
-- 嵌套包含争群,需要指定字段回怜,其中字段是target的字段
-- 0
select json_contains('{"a":1, "b":{"b1":"v1", "b2":"v2"}}', '{"b2":"v2"}');
-- 1
select json_contains('{"a":1, "b":{"b1":"v1", "b2":"v2"}}', '{"b2":"v2"}', '$.b');
json_arrayagg
將結(jié)果集聚合為json數(shù)組,該函數(shù)一般用在group by的命令上面换薄,這樣根據(jù)某個(gè)key聚合玉雾,其他的key就可以為數(shù)組形式了,這里用json_arrayagg就可以把那些值聚合為json數(shù)組
json_arrayagg(col_or_expr)
-- 建表
create table demo_json1(
`id` bigint unsigned not null auto_increment,
`num` int not null,
`json` json not null,
primary key(`id`)
)engine=innodb;
-- 添加數(shù)據(jù)
insert into demo_json1(`num`, `json`) values(1, '1');
insert into demo_json1(`num`, `json`) values(1, '{"a":1}');
insert into demo_json1(`num`, `json`) values(2, '{"a":1}');
insert into demo_json1(`num`, `json`) values(2, '{"a":2}');
insert into demo_json1(`num`, `json`) values(2, '{"a":3}');
-- 分組聚合
select `num`, json_arrayagg(`json`) as js from demo_json1 group by `num`;
json_objectagg
該函數(shù)用于將多個(gè)值聚集為一個(gè)json對(duì)象
json_objectagg(key, value)
說明:
其中key和value都是當(dāng)前的數(shù)據(jù)轻要,最后作為一個(gè)對(duì)象使用
CREATE TABLE `demo_json1` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`num` int NOT NULL,
`json` json NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
-- 數(shù)據(jù)
mysql> select * from demo_json1;
+----+-----+------+
| id | num | json |
+----+-----+------+
| 1 | 1 | "a" |
| 2 | 2 | "b" |
| 3 | 3 | "c" |
+----+-----+------+
-- 聚合查詢
{
"1": "a",
"2": "b",
"3": "c"
}
select json_objectagg(`num`,`json`) from demo_json1;
注意:
如果key對(duì)應(yīng)的值相同复旬,但是value對(duì)應(yīng)的值不同,則這個(gè)時(shí)候怎么辦冲泥,默認(rèn)是隨機(jī)的驹碍,按照查詢出來的順序指定,最后查到的就覆蓋前面的凡恍,如下
mysql> select * from demo_json1;
+----+-----+------+
| id | num | json |
+----+-----+------+
| 1 | 1 | "a" |
| 2 | 2 | "b" |
| 3 | 3 | "c" |
| 4 | 3 | "d" |
+----+-----+------+
-- 聚合查詢:
{
"1": "a",
"2": "b",
"3": "d"
}
select json_objectagg(`num`,`json`) from demo_json1;
如果我們要指定呢志秃,則可以通過函數(shù)over子句,這個(gè)子句是怎么用嚼酝,如下
-- over內(nèi)部為空浮还,則會(huì)返回所有的聚合
mysql> select json_objectagg(`num`,`json`) over() as js from demo_json1;
+--------------------------------+
| js |
+--------------------------------+
| {"1": "a", "2": "b", "3": "d"} |
| {"1": "a", "2": "b", "3": "d"} |
| {"1": "a", "2": "b", "3": "d"} |
| {"1": "a", "2": "b", "3": "d"} |
+--------------------------------+
-- 返回四條數(shù)據(jù),因?yàn)閿?shù)據(jù)內(nèi)部有四條數(shù)據(jù)
mysql> select * from demo_json1;
+----+-----+------+
| id | num | json |
+----+-----+------+
| 1 | 1 | "a" |
| 2 | 2 | "b" |
| 3 | 3 | "c" |
| 4 | 3 | "d" |
+----+-----+------+
我們可以給over子句內(nèi)部添加order by 進(jìn)行排序闽巩,這樣就有了順序了钧舌,其中order by 官網(wǎng)中說是按照如下進(jìn)行排序的
range between unbounded preceding and current row
mysql> select json_objectagg(`num`,`json`) over(order by `num`) as js from demo_json1;
+--------------------------------+
| js |
+--------------------------------+
| {"1": "a"} |
| {"1": "a", "2": "b"} |
| {"1": "a", "2": "b", "3": "d"} |
| {"1": "a", "2": "b", "3": "d"} |
+--------------------------------+
?涎跨?為啥顯示是這個(gè)延刘?官網(wǎng)上好像不是這樣顯示的,未知六敬,暫時(shí)遺留吧
json_merge_patch
該函數(shù)用于對(duì)多個(gè)文檔進(jìn)行合并
json_merge_patch(json_doc, json_doc[, json_doc] ...)
-- 在多個(gè)數(shù)據(jù)中有不是json對(duì)象的時(shí)候(包括json數(shù)組,json數(shù)組也認(rèn)為不是對(duì)象)驾荣,則返回最后一個(gè)
-- 2
select json_merge_patch('1', '2');
-- {"a": 1}
select json_merge_patch('1', '{"a":1}');
-- 2
select json_merge_patch('{"a":1}', '2');
-- 3
select json_merge_patch('{"a":1}', '2', '3');
-- {"c": 2}
select json_merge_patch('{"a":1}', '2', '3', '{"c":2}');
-- 若包含json數(shù)組外构,則也不是合并普泡,而是為最后一個(gè)
-- {"a": 1}
select json_merge_patch('[1, 2]', '{"a":1}');
-- [1, 2]
select json_merge_patch('{"a":1}', '[1, 2]');
-- [1, 3, 5]
select json_merge_patch('{"a":1}', '[1, 2]', '[1,3, 5]');
-- {"b": 2}
select json_merge_patch('{"a":1}', '[1, 2]', '[1,3, 5]', '{"b":2}');
-- [3, 4]
select json_merge_patch('[1, 2]', '[3, 4]');
-- 所有的都為對(duì)象,則進(jìn)行合并
-- {"a": 1, "c": 2}
select json_merge_patch('{"a":1}','{"c":2}');
-- 如果有key相同审编,則為最后一個(gè)撼班,不同的則合并
-- {"a": 2}
select json_merge_patch('{"a":1}','{"a":2}');
-- {"a": 3}
select json_merge_patch('{"a":1}','{"a":2}', '{"a":3}');
-- {"a": 2, "b": 3}
select json_merge_patch('{"a":1, "b":3}','{"a":2}');
json_array_append
給某些元素的值添加對(duì)應(yīng)的值
json_array_append(json_doc, path, val[, path, val] ...)
-- 對(duì)數(shù)組添加元素
-- [1]
select json_array_append('[]', '$', 1);
-- [1, 2, 3]
select json_array_append('[1]', '$', 2, '$', 3);
-- [[1, 1], 2, 3]
select json_array_append('[1, 2, 3]', '$[0]', 1);
-- 對(duì)對(duì)象添加數(shù)組元素
-- {"a": ["v", 1], "b": 1}
select json_array_append('{"a": "v", "b":1}', '$.a', 1);
-- [{"a": "v", "b": 1}, 1]
select json_array_append('{"a": "v", "b":1}', '$[0]', 1);
json_array_insert
該函數(shù)用于向已有的數(shù)組中添加對(duì)應(yīng)的值,這個(gè)值的下標(biāo)是函數(shù)的path指定的垒酬,指定后砰嘁,其他的值向后退
json_array_insert(json_doc, path, value[, path, value] ...)
-- [10, 0, 1, 2]
select json_array_insert('[0, 1, 2]', '$[0]', 10);
-- 沒有變化,因?yàn)樾枰禐閿?shù)組才行
select json_array_insert('{"a":1, "b":"v1"}', '$.a[0]', 10);
-- {"a": [10, 1], "b": "v1"}
select json_array_insert('{"a":[1], "b":"v1"}', '$.a[0]', 10);
json_storage_size
返回存儲(chǔ)的文檔的大小
json_storage_size(json_val)
-- 8
select json_storage_size('[1]');
-- 13
select json_storage_size('{"a":1}');
-- 21
select json_storage_size('{"a":1, "b":12}');
json_contains_path
該函數(shù)用于返回對(duì)應(yīng)的path是否存在
json_contains_path(json_doc, one_or_all, path[, path] ...)
-- one 表示后面的路徑中只要有一個(gè)匹配上就算找到
-- 1
select json_contains_path('{"a":1, "b":2}', 'one', '$.a');
-- 1
select json_contains_path('{"a":1, "b":2}', 'one', '$.a', '$.b');
-- 0
select json_contains_path('{"a":1, "b":2}', 'one', '$.c');
-- 1
select json_contains_path('{"a":1, "b":2}', 'one', '$.a', '$.c');
-- all 要求所有的path都能夠找到勘究,只要有一個(gè)不存在矮湘,則返回0
-- 1
select json_contains_path('{"a":1, "b":2}', 'all', '$.a');
-- 1
select json_contains_path('{"a":1, "b":2}', 'all', '$.a', '$.b');
-- 0
select json_contains_path('{"a":1, "b":2}', 'all', '$.c');
-- 0
select json_contains_path('{"a":1, "b":2}', 'all', '$.a', '$.c');
-- 嵌套內(nèi)部
-- 1
select json_contains_path('{"a":1, "b":2, "c":{"k1":"v1"}}', 'one', '$.c.k1');
json_merge_preserve
該函數(shù)跟json_merge_path有點(diǎn)像,但是merge_path是只有為對(duì)象才會(huì)合并口糕,而當(dāng)前這個(gè)函數(shù)沒有那么多限制缅阳,會(huì)將所有的數(shù)據(jù)都合并為數(shù)組
json_merge_preserve(json_doc, json_doc[, json_doc] ...)
-- 非對(duì)象的數(shù)據(jù)都會(huì)作為數(shù)組進(jìn)行合并,而對(duì)象的相同的key合并景描,value會(huì)合并為數(shù)組
-- ["a", "b"]
select json_merge_preserve('"a"', '"b"');
-- [1, "a"]
select json_merge_preserve('1', '"a"');
-- {"a": 1, "b": 2}
select json_merge_preserve('{"a":1}', '{"b":2}');
-- [{"a": 1}, 1]
select json_merge_preserve('{"a":1}', '[1]');
-- {"a": [1, 2], "b": 3}
select json_merge_preserve('{"a":1}', '{"a":2, "b":3}');
參考:
官網(wǎng)json函數(shù)文檔
https://dev.mysql.com/doc/refman/8.0/en/json-function-reference.html