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

mysql中json_extract的具體使用

 更新時間:2024年05月31日 11:04:52   作者:IT楓斗者  
mysql5.7版本開始支持JSON類型字段,本文主要介紹了mysql中json_extract的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

mysql5.7版本開始支持JSON類型字段,本文詳細介紹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路徑,其中 表示該 j s o n 數(shù)據(jù)本身, 表示該json數(shù)據(jù)本身, 表示該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路徑,其中 表示該 j s o n 數(shù)據(jù)本身, 表示該json數(shù)據(jù)本身, 表示該json數(shù)據(jù)本身,[i]表示獲取該json數(shù)組索引為i的元素(索引從0開始)

與獲取key-val一樣,若獲取的元素為字符串,默認的方式也會得到雙引號包起來的字符,導致程序轉(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ù)它作為判斷條件時使用

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

相關(guān)文章

  • MySQL中CREATE DATABASE語句創(chuàng)建數(shù)據(jù)庫的示例

    MySQL中CREATE DATABASE語句創(chuàng)建數(shù)據(jù)庫的示例

    在MySQL中,可以使用CREATE DATABASE語句創(chuàng)建數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • MySQL日期時間類型與字符串互相轉(zhuǎn)換的方法

    MySQL日期時間類型與字符串互相轉(zhuǎn)換的方法

    這篇文章主要介紹了MySQL日期時間類型與字符串互相轉(zhuǎn)換的方法,文中通過代碼示例和圖文結(jié)合的方式給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • MySql 修改密碼后的錯誤快速解決方法

    MySql 修改密碼后的錯誤快速解決方法

    今天在MySql5.6操作時報錯:You must SET PASSWORD before executing this statement解決方法,需要的朋友可以參考下
    2016-11-11
  • MySQL流程控制語句解讀

    MySQL流程控制語句解讀

    MySQL存儲過程支持多種流程控制語句,如IF、CASE、WHILE、LOOP、REPEAT等,每種語句有特定的執(zhí)行邏輯,用于實現(xiàn)條件判斷和循環(huán)操作
    2024-11-11
  • MySQL語句整理及匯總介紹

    MySQL語句整理及匯總介紹

    今天小編就為大家分享一篇關(guān)于MySQL語句整理及匯總介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • MySQL將select結(jié)果執(zhí)行update的實例教程

    MySQL將select結(jié)果執(zhí)行update的實例教程

    這篇文章主要給大家介紹了關(guān)于MySQL將select結(jié)果執(zhí)行update的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 有關(guān)mysql的一些小技巧

    有關(guān)mysql的一些小技巧

    有關(guān)mysql的一些小技巧,有需要的朋友可以參考下
    2013-02-02
  • windows下如何解決mysql secure_file_priv null問題

    windows下如何解決mysql secure_file_priv null問題

    這篇文章主要介紹了windows下如何解決mysql secure_file_priv null問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • MySQL需要根據(jù)特定順序排序的實現(xiàn)方法

    MySQL需要根據(jù)特定順序排序的實現(xiàn)方法

    在MySQL中,我們可以通過指定順序排序來在查詢結(jié)果中控制數(shù)據(jù)的排列順序,這種排序方式是非常有用的,本文就來介紹一下,感興趣的可以了解一下
    2023-11-11
  • mysql 5.7如何安裝 mysql 5.7安裝配置教程

    mysql 5.7如何安裝 mysql 5.7安裝配置教程

    這篇文章主要為大家詳細介紹了MySQL 5.7安裝配置方法,安裝過程中出現(xiàn)問題的解決方案,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論