当前位置:网站首页>MySQL中json_extract函数说明
MySQL中json_extract函数说明
2022-06-23 15:10:00 【江畔独步】
1. json_extract 使用场景说明
在日常业务开发中通常mysql数据库中某个字段会需要存储json格式字符串,查询的时候有时json数据较大,每次全部取出再去解析查询效率较低,也较麻烦.
好在Mysql5.7及之后的版本里提供了json_extract函数,可以通过key查询value值(如果是json数组类型,可以通过下标获取对应位置的值),非常方便。
2. MySQL json_extract 函数简介
2.1 函数简介
Mysql5.7版本以后新增的功能,Mysql提供了一个原生的Json类型,Json值将不再以字符串的形式存储,而是采用一种允许快速读取文本元素(document elements)的内部二进制(internal binary)格式。 在Json列插入或者更新的时候将会自动验证Json文本,未通过验证的文本将产生一个错误信息。 Json文本采用标准的创建方式,可以使用大多数的比较操作符进行比较操作,例如:=, <, <=, >, >=, <>, != 和 <=>。
2.2 使用方式
数据存储的数据是json字符串(类型是vachar)。
想要查询出来json中某个字段的值,用到方法是:JSON_EXTRACT()。
语法:**JSON_EXTRACT(json_doc, path[, path] …)**
用法提示:
- 如果json字符串不是数组,则直接使用
$.字段名 - 如果json字符串是数组[Array],则直接使用
$[对应的索引ID]
2.3 注意事项
JSON_EXTRACT性能验证 , 通过查看执行计划,验证全部都是全表扫描。
使用场景:数据量不大json字符串较大则可以采用,数据量较大不建议使用。
3. 数据验证
3.1 提取普通json中的值
说明:
- 普通字段使用
$.KEY获取 - 数组字段使用
$.KEY[index]获取,注意index从0开始 - 实际使用中,如下json字符串只需要换成对应的表字段即可,但要注意需对json判空和替换等ETL转换操作.
select
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.name") as name,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.tel_no") as tel_no,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[0]") as hobby_1,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[1]") as hobby_2,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[2]") as hobby_3,
json_extract('{"name":"zhangsan","tel_no":"136-6666-6666","hobbies":["basketball","run","sing"]}',"$.hobbies[3]") as hobby_4;
结果:
| name | tel_no | hobby_1 | hobby_2 | hobby_3 | hobby_4 |
|---|---|---|---|---|---|
| “zhangsan” | 136-6666-6666 | “basketball” | “run” | “sing” | NULL |
3.2 提取json数组的值
site_user表
| id | name | tags |
|---|---|---|
| 1 | zhangsan | [“COMMON”] |
| 2 | lisi | [“VIP”] |
| 3 | wangwu | [“VVIP”,“PLATINUM”] |
| 4 | zhaoliu |
提取用户的第一个标签:
select
id,
name,
tags,
json_extract(if(LENGTH(tags)>0,tags, '[]'),"$[0]") # 如果tags无数据,赋值为空数组
from site_user;
结果:
| id | name | tags |
|---|---|---|
| 1 | zhangsan | “COMMON” |
| 2 | lisi | “VIP” |
| 3 | wangwu | “VVIP” |
| 4 | zhaoliu | NULL |
边栏推荐
猜你喜欢

golang 重要知识:sync.Once 讲解

30. concatenate substrings of all words

Important knowledge of golang: sync Once explanation

js中的push函数介绍
Explain in detail the principle and implementation of redis distributed lock

golang 重要知识:waitgroup 解析

Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)

他山之石 | 微信搜一搜中的智能问答技术

golang 重要知识:RWMutex 读写锁分析
The idea and method of MySQL master-slave only synchronizing some libraries or tables
随机推荐
get_edges
TCP协议笔记
freemark 使用ftl文件 生成word
JS里的数组
Thymeleaf——学习笔记
Convert JSON file of labelme to coco dataset format
Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
Personal summary of system design and Analysis Course Project
Introduction to the push function in JS
Important knowledge of golang: atomic atomic operation
PHP指定字段大于100正序排,小于100随机排
直播间源码在开发前期必须做的工作及开发步骤
【无标题】激光焊接在医疗中的应用
JS garbage collection
Moher College - manual SQL injection vulnerability test (MySQL database)
一文看懂经典BUCK-BOOST负电压电路
golang 重要知识:sync.Cond 机制
js中的push函数介绍
List query sorting parameter processing
mysql 系列:存储引擎