mysql操作json优点和缺点_SQL-mysql操作json
一 前言
MySQL 5.7.8 之后 支持 JSON (由rfc7159規(guī)定)數(shù)據(jù)類型,其能在字段中使用json 類型,做到了自動(dòng)校驗(yàn)是否為json類型數(shù)據(jù),否則插入數(shù)據(jù)會(huì)報(bào)異常;其次,儲(chǔ)存json數(shù)據(jù)內(nèi)部做到了優(yōu)化儲(chǔ)存,能夠快速讀取json類型數(shù)據(jù),比如無需將二進(jìn)制json轉(zhuǎn)為文本形式后讀取;
公眾號:知識(shí)追尋者
知識(shí)追尋者(Inheriting the spirit of open source, Spreading technology knowledge;)
二 操作 JOSN
建表語句如下,為 area 字段 聲明為 JSON 類型;
CREATE TABLE `order` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '訂單編號',
`order_name` varchar(255) DEFAULT NULL COMMENT '訂單名稱',
`create_time` datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
`year` year(4) DEFAULT NULL COMMENT '年份',
`area` json DEFAULT NULL COMMENT '地區(qū)',
PRIMARY KEY (`id`),
UNIQUE KEY `order_name` (`order_name`,`create_time`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
MySQL 中支持 json 對象 和json 數(shù)組,他們之間可以相互嵌套;json類似map,在java中json就是map得實(shí)現(xiàn)類,初學(xué)者若不懂何為json,就當(dāng)作map來用,即儲(chǔ)存 key - value 形式得數(shù)據(jù)結(jié)構(gòu);注意點(diǎn)是 json 數(shù)據(jù) 得key 必須是字符串,可以有key無value;
2.1 插入 josn數(shù)據(jù)
插入 json 對象,east 得值 為 50 , sourth 值為65 得 json對象;
INSERT INTO `order`(`order_name`, `year`, `area`)
VALUES ('荷小花的訂單', 2020, '{"east": "50", "south": "65"}');
等效于使用 JSON_OBJECT 函數(shù), 示例如下
INSERT INTO `order`(`order_name`, `year`, `area`)
VALUES ('荷小花的訂單', 2020, JSON_OBJECT("east", "50", "south", "65"));
插入 json 數(shù)組
INSERT INTO `order`(`order_name`, `year`, `area`)
VALUES ('荷小花的訂單', 2020, '[{"east": "50", "south": "65"}]');
等效于使用 JSON_ARRAY 函數(shù),示例如下
INSERT INTO `order`(`order_name`, `year`, `area`)
VALUES ('荷小花的訂單', 2020, JSON_ARRAY("east", "50", "south", "65"));
2.2 查詢json數(shù)據(jù)
使用 column - path 路徑符 -> 查詢 指定key 得值,
select order_name, area -> '$.east' from `order`
注意,如果json 數(shù)據(jù)中不存在 east 得鍵,則列出為null值
輸出如下
--------- ------
荷小花的訂單"50"
荷小花的訂單
也可以使用 ->> 符號, 不同之處是使用 ->> 更加直觀,輸出得json數(shù)據(jù)最外層不會(huì)攜帶雙引號,內(nèi)層數(shù)據(jù)中存在雙引號不會(huì)有反斜杠轉(zhuǎn)義;
select order_name, area ->> '$.east' from `order`
使用 單引號 代替 雙引號
select order_name, area ->> "$.east" from `order`
我們 也可以使用 JSON_EXTRACT 函數(shù) 達(dá)到同樣得效果;
SELECT order_name,JSON_EXTRACT(area, '$.east') from `order`;
2.3 修改json數(shù)據(jù)
使用 JSON_SET 設(shè)置 json key 得 值
id = 1 得 area 數(shù)據(jù)如下
{"north": "55", "south": "66"}
現(xiàn)在將south 值改為 60 的語句示例如下
update `order` set area = json_set(area, '$[0].south', '60') where id = '1'
其中 $[0] 代表 json 中的第一個(gè)對象 ,以此類推 $[1] 為 josn 中的第二個(gè)對象;
示例
["6","2",{"east": "50", "south": "65"}]
$[0] 為"6",$[1] 為 "2" , $[2] 為 {"east": "50", "south": "65"}; $[2].east 為 "50" , 或者 $[2][1];
如果上面$[*] 表達(dá)式式理解困難也可以使用如下方式
update `order` set area = json_set(area, '$.south', '60') where id = '1'
tip: 如果更改整個(gè)json值 與 平時(shí)的更新數(shù)據(jù)方式一致
2.4 刪除json中的數(shù)據(jù)
使用 json_remove 可以達(dá)到效果;
示例: 刪除json 中的 south 鍵
update `order` set area =json_remove(area, '$.south') where id = '1'
三 jsom函數(shù)
3.1 cast
cast 函數(shù) 是特殊函數(shù),可以使用 CAST(expr AS type) 函數(shù)進(jìn)行數(shù)據(jù)類型得轉(zhuǎn)換,此函數(shù) 與 convert 用法 類似 ,即 期望得表達(dá)式轉(zhuǎn)為期望得類型;
比如 將 字符串 知識(shí)追尋者從默認(rèn)類型轉(zhuǎn)為utf8類型
SELECT CONVERT('知識(shí)追尋者' USING utf8);
如下情況下查詢是字符串,非json數(shù)據(jù)
select '{"east": "50", "south": "65"}' as str
再來看看 cast 函數(shù)使用,將字符串轉(zhuǎn)為json 類型
select cast( '{"east": "50", "south": "65"}' as json )
3.2 JSON_TYPE
JSON_TYPE 函數(shù) 會(huì)嘗試 去解析 參數(shù)為json值,
示例
select JSON_TYPE('{"east": "50", "south": "65"}')
得到結(jié)果為json 對象
OBJECT
示例
select JSON_TYPE('[{"east": "50", "south": "65"}]')
得到結(jié)果為json數(shù)組
ARRAY
示例
select JSON_TYPE('hello')
輸出為異常,無效得json
Invalid JSON text in argument 1 to function json_type: "Invalid value." at position 0.
3.3 JSON_MERGE
JSON_MERGE 函數(shù) 即 將合并多個(gè)json文檔; 合并規(guī)則如下
如果都是json array,合并為json array;
如果都是json object,合并為json object;
如果有多種類型數(shù)據(jù),則將非json array的元素封裝成json array再按照如上規(guī)則進(jìn)行合并;
示例
select json_merge('["west","20"]', '{"east": "50", "south": "65"}')
輸出
["west", "20", {"east": "50", "south": "65"}]
3.4 JSON_VALID
JSON_VALID 函數(shù) 為 校驗(yàn)是否是json 函數(shù),是返回 1 ,否則 返回0;
示例如下,返回1;
SELECT JSON_VALID('{"east": "50", "south": "65"}')
示例如下,返回 0;
SELECT JSON_VALID('hello')
3.5 JSON_INSERT
JSON_INSERT 函數(shù) 向 json 中添加新的值,不會(huì)改變已經(jīng)存在的值;
id = 1 的數(shù)據(jù)如下
{"north": "55"}
更新語句如下
update `order` set area =JSON_INSERT(area, '$.north', 55 ,'$.south', "60") where id = '1'
更改結(jié)果如下
{"north": "55", "south": "60"}
3.6 JSON_REPLACE
JSON_REPLACE 替換現(xiàn)有的值,如果存在新的值不會(huì)添加;
id = 1 的數(shù)據(jù)如下
{"north": "55", "south": "60"}
更新語句如下
update `order` set area =JSON_REPLACE (area, '$.north', "50" ,'$.east', "60")
where id = '1'
更改結(jié)果如下
{"north": "50", "south": "60"}
3.7 JSON_SEARCH
JSON_SEARCH , 返回路徑,支持返回單個(gè)和返回多個(gè);
id = 1 的數(shù)據(jù)如下
[{"north": "50", "south": "60"}, {"north": "50", "south": "70"}]
查詢 一個(gè)值為 50 的key路徑
select JSON_SEARCH(area, 'one', '50') from `order` where id = '1'
輸出
"$[0].north"
查詢 所有值為 50 的key路徑
select JSON_SEARCH(area, 'all', '50') from `order` where id = '1'
輸出
["$[0].north", "$[1].north"]
3.8 JSON_KEYS
返回json頂級值底下所有key
id = 1 的數(shù)據(jù)如下
[{"north": "50", "south": "60"}, {"north": "50", "south": "70"}]
查詢json數(shù)據(jù)中第一個(gè)json對象所有的key
select JSON_KEYS(area, '$[0]') from `order` where id = '1'
輸出
["north", "south"]
總結(jié)
以上是生活随笔為你收集整理的mysql操作json优点和缺点_SQL-mysql操作json的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: propertysource注解_Jav
- 下一篇: c mysql 统计不重复数据库,MyS