当前位置:网站首页>MySql之json_extract函数处理json字段
MySql之json_extract函数处理json字段
2022-08-10 05:09:00 【小灰灰Blog】
在db中存储json格式的数据,相信大家都或多或少的使用过,那么在查询这个json结构中的数据时,有什么好的方法么?取出String之后再代码中进行解析?
接下来本文将介绍一下Mysql5.7+之后提供的json_extract函数,可以通过key查询value值
<!-- more -->
1. 使用方式
数据存储的数据是json字符串,类型为我们常用的varchar即可
语法:
JSON_EXTRACT(json_doc, path[, path] …)
若json字符串非数组时,可以通过$.字段名
来表示查询对应的value
2.使用演示
创建一个测试的表
CREATE TABLE `json_table` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `val` json DEFAULT NULL COMMENT 'json字符串', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
插入几条数据
insert into `json_table` values (1, '{"name": "一灰灰blog", "age": 18}');insert into `json_table` values (2, '{"name": "一灰灰blog", "site": "https://blog.hhui.top"}');
查询json串中的name
,如下
mysql> select json_extract(`val`, '$.name') from `json_table`;+-------------------------------+| json_extract(`val`, '$.name') |+-------------------------------+| "一灰灰blog" || "一灰灰blog" |+-------------------------------+
如果查询的key不在json串中,返回的是null,而不是抛异常
mysql> select json_extract(`val`, '$.name') as `name`, json_extract(`val`, '$.site') as `site` from `json_table`;+-----------------+-------------------------+| name | site |+-----------------+-------------------------+| "一灰灰blog" | NULL || "一灰灰blog" | "https://blog.hhui.top" |+-----------------+-------------------------+
接下来再看一下如果为json数组,怎么整
mysql> insert into `json_table` values (3, '[{"name": "一灰灰", "site": "https://spring.hhui.top"}]');mysql> select json_extract(`val`, '$[0].name') from `json_table` where id = 3;+----------------------------------+| json_extract(`val`, '$[0].name') |+----------------------------------+| "一灰灰" |+----------------------------------+
除了在查询结果中使用json_extract之外,也可以在查询条件中使用它
mysql> select * from `json_table` where json_extract(`val`, '$.name') = '一灰灰blog';+----+------------------------------------------------------------+| id | val |+----+------------------------------------------------------------+| 1 | {"age": 18, "name": "一灰灰blog"} || 2 | {"name": "一灰灰blog", "site": "https://blog.hhui.top"} |+----+------------------------------------------------------------+
3. 小结
本文主要介绍json_extract函数的使用姿势,用于解析字段内value为json串的场景
基本使用姿势
- json对象:json_extract('db字段', '$.json串key')
- json数组:json_extract('db字段', '$[数组下标].json串key')
一灰灰的联系方式
尽信书则不如无书,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
- 个人站点:https://blog.hhui.top
- 微博地址: 小灰灰Blog
- QQ: 一灰灰/3302797840
- 微信公众号:一灰灰blog
边栏推荐
- FPGA engineer interview questions collection 11~20
- 如何取得某月的最后一天
- How does flinksql write that the value of redis has only the last field?
- Zhongang Mining: Strong downstream demand for fluorite
- ORA-16018 异常处理记录
- 文献 | 关于心理活动符号学,你知道多少?
- The sword refers to Offer 033. Variation array
- 开发智能硬件过程中需要掌握的方法之经典
- 解决“File has been changed outside the editor, reload?”提示
- Ueditor editor arbitrary file upload vulnerability
猜你喜欢
深度梳理:防止模型过拟合的方法汇总
Joomla vulnerability reproduced
Stacks and Queues | Valid parentheses, delete all adjacent elements in a string, reverse Polish expression evaluation, maximum sliding window, top K high frequency elements | leecode brush questions
顺序表的删除,插入和查找操作
Joomla漏洞复现
安芯电子IPO过会:年营收4亿 汪良恩兄弟持股61.6%
How cursors work in Pulsar
Ueditor editor arbitrary file upload vulnerability
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
万字总结:分布式系统的38个知识点
随机推荐
SQL数据库字段追加到主表
大佬们,mysql cdc(2.2.1跟之前的版本)从savepoint起有时出现这种情况,有没有什
元宇宙 | 你能通过图灵测试吗?
canvas canvas drawing clock
深度学习之-01
Thread.sleep, Thread.yield role explanation
Concurrency tool class - introduction and use of CountDownLatch, CyclicBarrier, Semaphore, Exchanger
深度梳理:防止模型过拟合的方法汇总
2022 R2 transportable pressure vessel filling operation examination question bank simulation platform
The time for flinkcdc to read pgsql is enlarged. Does anyone know what happened? gmt_create':1
Become a language that hackers have to learn. Do you think it's okay after reading it?
什么是“大小端字节序”存储模式?
单页面应用
ThreadPoolExecutor线程池原理
How to get the last day of a month
十年架构五年生活-07 年轻气盛的蜕变
应用在智能触摸遥控器中的触摸芯片
pytorch 学习
FPGA工程师面试试题集锦11~20
小影科技IPO被终止:年营收3.85亿 五岳与达晨是股东