關(guān)于mysql中的json解析函數(shù)JSON_EXTRACT
mysql json解析函數(shù)JSON_EXTRACT
MYSQl自帶的解析函數(shù)JSON_EXTRACT,用JSON_EXTRACT函數(shù)解析出來(lái)的函數(shù)會(huì)包含雙引號(hào)
例子
t_table表里面source字段是以json格式寫(xiě)的值為
{ "info" : { "color" : "白色", "inner_color" : "米色", "number" : "12345678", "registration_date" : "2012-11" },
"accessory" : [ "1", "4", "5", "6", "7", "8", "9", "10" ],
"remark" : "測(cè)試"}查詢color不去掉雙引號(hào),inner_color去掉雙引號(hào),remark去掉雙引
select? source->'$.info.color' ?as color, replace(source->'$.info.inner_color','"','') ? as inner_color, replace(source->'$.remark','"','') ? as remark ?from t_table
查詢結(jié)果
| color | inner_color | remark |
|---|---|---|
| “白色” | 米色 | 測(cè)試 |
mysql5.7 json格式與json_extract方法
數(shù)據(jù)初始化
json_test表數(shù)據(jù),id和jsonstr字段(類型json)
{
"no": "7",
"title": "運(yùn)動(dòng)方式",
"content": [{
"text": "您平時(shí)經(jīng)常進(jìn)行的運(yùn)動(dòng)及時(shí)間",
"item1": [{
"text": "慢跑 / 快走 / 走跑結(jié)合",
"type": "select",
"value": "selected"
}, {
"text": "上下樓梯 / 爬山",
"type": "multselect",
"value": "selected"
}],
"item2": [{
"text": "慢跑222走跑結(jié)合",
"type": "text",
"value": "慢跑2"
}, {
"text": "上下樓梯 / 爬山2",
"type": "number",
"value": "33"
}]
}]
}select語(yǔ)句
SELECT ? ? id, ? ? json_extract( t.jsonstr, '$.*' ), ? ? json_extract( t.jsonstr, '$.title' ) AS "title", ? ? json_extract( t.jsonstr, '$.content' ) AS "content" , ? ? json_extract( t.jsonstr, '$**.text' ) AS "text" , ? ? json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1"? FROM ? ? json_test t;
返回結(jié)果解析
//json_extract( t.jsonstr, '$.*' )返回:
["7", "運(yùn)動(dòng)方式", [{"text": "您平時(shí)經(jīng)常進(jìn)行的運(yùn)動(dòng)及時(shí)間", "item1": [{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑結(jié)合", "type": "text", "value": "慢跑2"}, {"text": "上下樓梯 / 爬山2", "type": "number", "value": "33"}]}]]
//json_extract( t.jsonstr, '$.title' ) AS "title"返回:
"運(yùn)動(dòng)方式"
//json_extract( t.jsonstr, '$.content' ) AS "content" 返回:
[{"text": "您平時(shí)經(jīng)常進(jìn)行的運(yùn)動(dòng)及時(shí)間", "item1": [{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}], "item2": [{"text": "慢跑222走跑結(jié)合", "type": "text", "value": "慢跑2"}, {"text": "上下樓梯 / 爬山2", "type": "number", "value": "33"}]}]
//json_extract( t.jsonstr, '$**.text' ) AS "text" 返回:
["您平時(shí)經(jīng)常進(jìn)行的運(yùn)動(dòng)及時(shí)間", "慢跑 / 快走 / 走跑結(jié)合", "上下樓梯 / 爬山", "慢跑222走跑結(jié)合", "上下樓梯 / 爬山2"]
//json_extract( t.jsonstr, '$.content[*].item1[*]' ) AS "item1" 返回:
[{"text": "慢跑 / 快走 / 走跑結(jié)合", "type": "select", "value": "selected"}, {"text": "上下樓梯 / 爬山", "type": "multselect", "value": "selected"}]用法解析
| ‘$.*’ | 返回全部json |
| ‘$.title’ | 返回key=”title”的數(shù)據(jù) |
| ‘$**.text’ | 返回所有最底層key=”text”的數(shù)據(jù) |
| ‘$.content[*].item1[*]’ | 返回key=content的list的key=item1的list的所有內(nèi)容 |
官方文檔:https://dev.mysql.com/doc/refman/5.7/en/json.html
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mysql 主從復(fù)制如何跳過(guò)報(bào)錯(cuò)
這篇文章主要介紹了mysql 主從復(fù)制如何跳過(guò)報(bào)錯(cuò),幫助大家更好的理解和使用MySQL 數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2020-10-10
mysql開(kāi)啟遠(yuǎn)程連接(mysql開(kāi)啟遠(yuǎn)程訪問(wèn))
開(kāi)啟MYSQL遠(yuǎn)程連接權(quán)限的方法,大家參考使用吧2013-12-12
textarea標(biāo)簽(存取數(shù)據(jù)庫(kù)mysql)的換行方法
textarea標(biāo)簽本身不識(shí)別換行功能,回車換行用的是\n換行符,輸入時(shí)的確有換行的效果,但是html渲染或者保存數(shù)據(jù)庫(kù)mysql時(shí)就只是一個(gè)空格了,這時(shí)就需要利用換行符\n和br標(biāo)簽的轉(zhuǎn)換進(jìn)行處理2023-09-09
mysql中一個(gè)普通ERROR 1135 (HY000)錯(cuò)誤引發(fā)的血案
ERROR 1135 (HY000): Can’t create a new thread (errno 11);if you are not out of available memory,you can consult the manual for a possible OS-dependent bug2015-08-08
Mysql數(shù)據(jù)庫(kù)開(kāi)啟遠(yuǎn)程連接流程
文章講述了如何在本地MySQL數(shù)據(jù)庫(kù)上開(kāi)啟遠(yuǎn)程訪問(wèn),并詳細(xì)步驟包括配置防火墻、設(shè)置MySQL用戶權(quán)限、使用Navicat進(jìn)行遠(yuǎn)程連接等2025-02-02
MySql 快速插入千萬(wàn)級(jí)大數(shù)據(jù)的方法示例
這篇文章主要介紹了MySql 快速插入千萬(wàn)級(jí)大數(shù)據(jù)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

