欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mysql中json_extract的使用方法實例詳解

 更新時間:2023年04月13日 09:14:09   作者:-王尚可-  
MYSQl自帶的解析函數(shù)JSON_EXTRACT,用JSON_EXTRACT函數(shù)解析出來的函數(shù)會包含雙引號,下面這篇文章主要給大家介紹了關(guān)于mysql中json_extract的使用方法,需要的朋友可以參考下

一、前言

mysql5.7版本開始支持JSON類型字段,本文詳細(xì)介紹json_extract函數(shù)如何獲取mysql中的JSON類型數(shù)據(jù)
json_extract可以完全簡寫為 ->
json_unquote(json_extract())可以完全簡寫為 ->>
下面介紹中大部分會利用簡寫

二、創(chuàng)建示例表

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"}]');
idcontent
1{“age”: 18, “name”: “tom”, “score”: [100, 90, 87], “address”: {“city”: “長沙”, “province”: “湖南”}}
2[1, “apple”, “red”, {“age”: 18, “name”: “tom”}]

三、基本語法

- 獲取JSON對象中某個key對應(yīng)的value值

  • json_extract函數(shù)中,第一個參數(shù)content表示json數(shù)據(jù),第二個參數(shù)為json路徑,其中$表示該json數(shù)據(jù)本身,$.name就表示獲取json中key為name的value值
  • 可以利用 -> 表達式來代替json_extract
  • 若獲取的val本身為字符串,那么獲取的val會被引號包起來,比如"tom",這種數(shù)據(jù)被解析到程序?qū)ο笾袝r,可能會被轉(zhuǎn)義為\“tom\”。為了解決這個問題了,可以在外面再包上一層json_unquote函數(shù),或者使用 ->> 代替->

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;
# 結(jié)果:
+--------------------------------+
| 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;
# 結(jié)果:
+----------------------------------------------+
| json_unquote(json_extract(content,'$.name')) |
+----------------------------------------------+
| tom                                          |
+----------------------------------------------+
+--------------------+
| content->>'$.name' |
+--------------------+
| tom                |
+--------------------+

- 獲取JSON數(shù)組中某個元素

  • json_extract函數(shù)中,第一個參數(shù)content表示json數(shù)據(jù),第二個參數(shù)為json路徑,其中$表示該json數(shù)據(jù)本身,$[i]表示獲取該json數(shù)組索引為i的元素(索引從0開始)
  • 與獲取key-val一樣,若獲取的元素為字符串,默認(rèn)的方式也會得到雙引號包起來的字符,導(dǎo)致程序轉(zhuǎn)義,方法也是利用json_unquote函數(shù),或者使用 ->> 代替->

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;
# 結(jié)果:
+------------------------------+
| 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;
# 結(jié)果:
+--------------------------------------------+
| json_unquote(json_extract(content,'$[1]')) |
+--------------------------------------------+
| apple                                      |
+--------------------------------------------+
+------------------+
| content->>'$[1]' |
+------------------+
| apple            |
+------------------+

- 獲取JSON中的嵌套數(shù)據(jù)

結(jié)合前面介紹的兩種獲取方式,可以獲取json數(shù)據(jù)中的嵌套數(shù)據(jù)

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;
# 結(jié)果:
+-----------------------+
| content->'$.score[2]' |
+-----------------------+
| 87                    |
+-----------------------+

# 得到:18
select content->'$[3].age' from test_json where id = 2;
# 結(jié)果:
+---------------------+
| content->'$[3].age' |
+---------------------+
| 18                  |
+---------------------+

四、漸入佳境

- 獲取JSON多個路徑的數(shù)據(jù)

將會把多個路徑的數(shù)據(jù)組合成數(shù)組返回

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;
# 結(jié)果:
+-----------------------------------------+
| json_extract(content,'$.age','$.score') |
+-----------------------------------------+
| [18, [100, 90, 87]]                     |
+-----------------------------------------+

select json_extract(content,'$.name','$.address.province','$.address.city') from test_json where id = 1;
# 結(jié)果:
+----------------------------------------------------------------------+
| json_extract(content,'$.name','$.address.province','$.address.city') |
+----------------------------------------------------------------------+
| ["tom", "湖南", "長沙"]                                              |
+----------------------------------------------------------------------+

- 路徑表達式*的使用

將會把多個路徑的數(shù)據(jù)組合成數(shù)組返回

# 先插入一條用于測試的數(shù)據(jù)
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的二級嵌套是一個數(shù)組,所以.name獲取不到其中的所有name值
select content->'$.*.name' from test_json where id = 3;
+----------------------------------+
| content->'$.*.name'              |
+----------------------------------+
| ["一年三班", "中央公園"]         |
+----------------------------------+```

# 獲取所有key為name值的數(shù)據(jù),包括任何嵌套內(nèi)的name
select content->'$**.name' from test_json where id = 3;
+---------------------------------------------------------+
| content->'$**.name'                                     |
+---------------------------------------------------------+
| ["tom", "一年三班", "marry", "Bob", "中央公園"]         |
+---------------------------------------------------------+

# 獲取數(shù)組中所有的name值
select content->'$.friend[*].name' from test_json where id = 3;
+-----------------------------+
| content->'$.friend[*].name' |
+-----------------------------+
| ["marry", "Bob"]            |
+-----------------------------+

- 返回NULL值

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                               |
+------------------------------------+

- 返回錯誤

若第一個參數(shù)不是JSON類型的數(shù)據(jù),則返回錯誤

select json_extract('{1,2]',$[0])

若路徑表達式不規(guī)范,則返回錯誤

select content->'$age' from test_json where id = 1;
# 結(jié)果:
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 1.

五、使用場景

JSON_EXTRACT函數(shù)通常用于要獲取JSON中某個特定的數(shù)據(jù)或者要根據(jù)它作為判斷條件時使用

六、參考文檔

總結(jié)

到此這篇關(guān)于mysql中json_extract使用方法的文章就介紹到這了,更多相關(guān)mysql中json_extract使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論