您好,登錄后才能下訂單哦!
這篇文章主要介紹“mysql中的json_extract怎么使用”,在日常操作中,相信很多人在mysql中的json_extract怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql中的json_extract怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
mysql5.7版本開始支持JSON類型字段
json_extract可以完全簡寫為 ->
json_unquote(json_extract())可以完全簡寫為 ->>
下面介紹中大部分會利用簡寫
CREATE TABLE `test_json` ( `id` int(11) NOT NULL AUTO_INCREMENT, `content` json DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
# 插入兩條測試用的記錄 INSERT INTO `test_json` (`content`) VALUES ('{\"name\":\"tom\",\"age\":18,\"score\":[100,90,87],\"address\":{\"province\":\"湖南\",\"city\":\"長沙\"}}'); INSERT INTO `test_json` (`content`) VALUES ('[1, "apple", "red", {"age": 18, "name": "tom"}]');
id | content |
---|---|
1 | {“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}} |
2 | [1, “apple”, “red”, {“age”: 18, “name”: “tom”}] |
json_extract函數中,第一個參數content表示json數據,第二個參數為json路徑,其中$表示該json數據本身,$.name就表示獲取json中key為name的value值
可以利用 -> 表達式來代替json_extract
若獲取的val本身為字符串,那么獲取的val會被引號包起來,比如"tom",這種數據被解析到程序對象中時,可能會被轉義為\“tom\”。為了解決這個問題了,可以在外面再包上一層json_unquote函數,或者使用 ->> 代替->
content:
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}}
# 得到"tom" select json_extract(content,'$.name') from test_json where id = 1; # 簡寫方式:字段名->表達式等價于json_extract(字段名,表達式) select content->'$.name' from test_json where id = 1; # 結果: +--------------------------------+ | json_extract(content,'$.name') | +--------------------------------+ | "tom" | +--------------------------------+ +-------------------+ | content->'$.name' | +-------------------+ | "tom" | +-------------------+ # 解除雙引號,得到tom select json_unquote(json_extract(content,'$.name')) from test_json where id = 1; # 簡寫方式:字段名->>表達式等價于json_unquote(json_extract(字段名,表達式)) select content->>'$.name' from test_json where id = 1; # 結果: +----------------------------------------------+ | json_unquote(json_extract(content,'$.name')) | +----------------------------------------------+ | tom | +----------------------------------------------+ +--------------------+ | content->>'$.name' | +--------------------+ | tom | +--------------------+
json_extract函數中,第一個參數content表示json數據,第二個參數為json路徑,其中$表示該json數據本身,$[i]表示獲取該json數組索引為i的元素(索引從0開始)
與獲取key-val一樣,若獲取的元素為字符串,默認的方式也會得到雙引號包起來的字符,導致程序轉義,方法也是利用json_unquote函數,或者使用 ->> 代替->
content:
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]
# 得到"apple" select json_extract(content,'$[1]') from test_json where id = 2; # 簡寫,效果同上 select content->'$[1]' from test_json where id = 2; # 結果: +------------------------------+ | json_extract(content,'$[1]') | +------------------------------+ | "apple" | +------------------------------+ +-----------------+ | content->'$[1]' | +-----------------+ | "apple" | +-----------------+ # 解除雙引號,得到apple select json_unquote(json_extract(content,'$[1]')) from test_json where id = 2; # 簡寫,效果同上 select content->>'$[1]' from test_json where id = 2; # 結果: +--------------------------------------------+ | json_unquote(json_extract(content,'$[1]')) | +--------------------------------------------+ | apple | +--------------------------------------------+ +------------------+ | content->>'$[1]' | +------------------+ | apple | +------------------+
結合前面介紹的兩種獲取方式,可以獲取json數據中的嵌套數據
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}}
content: id=2
[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]
# 得到:87 select content->'$.score[2]' from test_json where id = 1; # 結果: +-----------------------+ | content->'$.score[2]' | +-----------------------+ | 87 | +-----------------------+ # 得到:18 select content->'$[3].age' from test_json where id = 2; # 結果: +---------------------+ | content->'$[3].age' | +---------------------+ | 18 | +---------------------+
將會把多個路徑的數據組合成數組返回
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}}
select json_extract(content,'$.age','$.score') from test_json where id = 1; # 結果: +-----------------------------------------+ | json_extract(content,'$.age','$.score') | +-----------------------------------------+ | [18, [100, 90, 87]] | +-----------------------------------------+ select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1; # 結果: +----------------------------------------------------------------------+ | json_extract(content,'$.name','$.address.province','$.address.city') | +----------------------------------------------------------------------+ | ["tom", "湖南", "長沙"] | +----------------------------------------------------------------------+
將會把多個路徑的數據組合成數組返回
# 先插入一條用于測試的數據 INSERT INTO `test_json` (`id`,`content`) VALUES(3,'{"name":"tom","address":{"name":"中央公園","city":"長沙"},"class":{"id":3,"name":"一年三班"},"friend":[{"age":20,"name":"marry"},{"age":21,"name":"Bob"}]}')
content: id=3
{“name”: “tom”, “class”: {“id”: 3, “name”: “一年三班”}, “friend”: [{“age”: 20, “name”: “marry”}, {“age”: 21, “name”: “Bob”}], “address”: {“city”: “長沙”, “name”: “中央公園”}}
# 獲取所有二級嵌套中key=name的值 # 由于friend的二級嵌套是一個數組,所以.name獲取不到其中的所有name值 select content->'$.*.name' from test_json where id = 3; +----------------------------------+ | content->'$.*.name' | +----------------------------------+ | ["一年三班", "中央公園"] | +----------------------------------+``` # 獲取所有key為name值的數據,包括任何嵌套內的name select content->'$**.name' from test_json where id = 3; +---------------------------------------------------------+ | content->'$**.name' | +---------------------------------------------------------+ | ["tom", "一年三班", "marry", "Bob", "中央公園"] | +---------------------------------------------------------+ # 獲取數組中所有的name值 select content->'$.friend[*].name' from test_json where id = 3; +-----------------------------+ | content->'$.friend[*].name' | +-----------------------------+ | ["marry", "Bob"] | +-----------------------------+
content: id=1
{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}}
尋找的JSON路徑都不存在
# age路徑不存在,返回NULL # 若有多個路徑,只要有一個路徑存在則不會返回NULL select json_extract(content,'$.price') from test_json where id = 1; +---------------------------------+ | json_extract(content,'$.price') | +---------------------------------+ | NULL | +---------------------------------+
路徑中有NULL
# 存在任意路徑為NULL則返回NULL select json_extract(content,'$.age',NULL) from test_json where id = 1; +------------------------------------+ | json_extract(content,'$.age',NULL) | +------------------------------------+ | NULL | +------------------------------------+
若第一個參數不是JSON類型的數據,則返回錯誤
select json_extract('{1,2]',$[0])
若路徑表達式不規范,則返回錯誤
select content->'$age' from test_json where id = 1; # 結果: ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.
JSON_EXTRACT函數通常用于要獲取JSON中某個特定的數據或者要根據它作為判斷條件時使用
到此,關于“mysql中的json_extract怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。