当前位置:网站首页>MySql's json_extract function processes json fields
MySql's json_extract function processes json fields
2022-08-10 05:17:00 【Small gray Blog】
Storing data in json format in db, I believe everyone has used it more or less, so is there any good way to query the data in this json structure?After taking out the String, do the parsing in the code?
Next, this article will introduce the json_extract function provided after Mysql5.7+, which can query the value value by key
1. How to use
The data stored in the data is a json string, and the type is our commonly used varchar
Syntax:
JSON_EXTRACT(json_doc, path[, path] …)If the json string is not an array, you can use $.field name to indicate the value corresponding to the query
2. Use the demo
Create a test table
CREATE TABLE `json_table` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'primary key id', `val` json DEFAULT NULL COMMENT 'json string', PRIMARY KEY(`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;Insert a few pieces of data
insert into `json_table` values (1, '{"name": "a gray blog", "age": 18}');insert into `json_table` values(2, '{"name": "A gray blog", "site": "https://blog.hhui.top"}');Query the name in the json string, as follows
mysql> select json_extract(`val`, '$.name') from `json_table`;+-------------------------------+| json_extract(`val`, '$.name') |+-------------------------------+| "A gray blog" || "A gray blog" |+---------------------------------+If the query key is not in the json string, null is returned instead of throwing an exception
mysql> select json_extract(`val`, '$.name') as `name`, json_extract(`val`, '$.site') as `site` from`json_table`;+-----------------+-------------------------+|name | site |+-----------------+-------------------------+|"One Ash Blog" | NULL || "One Ash Blog" | "https://blog.hhui.top" |+-----------------+-------------------------+Let's take a look at how to adjust if it is a json array
mysql> insert into `json_table` values (3, '[{"name": "One gray gray", "site": "https://spring.hhui.top"}]');mysql> select json_extract(`val`, '$[0].name') from `json_table` where id = 3;+----------------------------------+| json_extract(`val`, '$[0].name') |+----------------------------------+| "A grayish gray" |+-------------------------------------+In addition to using json_extract in query results, it can also be used in query conditions
mysql> select * from `json_table` where json_extract(`val`, '$.name') = 'a gray blog';+----+------------------------------------------------------------+| id | val |+----+--------------------------------------------------------------------------+| 1 | {"age": 18, "name": "A gray blog"} || 2 | {"name": "Huihui blog", "site": "https://blog.hhui.top"} |+----+------------------------------------------------------------+3. Summary
This article mainly introduces the usage of the json_extract function, which is used to parse the scene where the value in the field is a json string
Basic posture
- json object: json_extract('db field', '$.json string key')
- json array: json_extract('db field', '$[array subscript].json string key')
A grey contact information
It is better to have no books than no books. The above content is purely from one family. Due to limited personal ability, there are inevitably omissions and mistakes. If you find bugs or have better suggestions, criticisms and corrections are welcome, and I will be grateful
- Personal site: https://blog.hhui.top
- Weibo address: Xiaohuihui Blog
- QQ: A gray gray/3302797840
- WeChat Official Account: A Grey Blog

边栏推荐
- 【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation
- JavsSE => 多态
- Attention candidates for the soft exam! The detailed registration process for the second half of 2022 is coming!
- pytorch learning
- awk of the Three Musketeers of Shell Programming
- The sword refers to Offer 033. Variation array
- 大佬们,mysql cdc(2.2.1跟之前的版本)从savepoint起有时出现这种情况,有没有什
- Abstract problem methodology
- 8.STM32F407之HAL库——PWM笔记
- `id` bigint(20) unsigned NOT NULL COMMENT '数据库主键',
猜你喜欢

Arduino框架下合宙ESP32C3 +1.8“tft 网络时钟

成为黑客不得不学的语言,看完觉得你们还可吗?

【论文笔记】Prototypical Contrast Adaptation for Domain Adaptive Semantic Segmentation

【LeetCode】41. The first missing positive number

暑期学前作业

Jenkins 如何玩转接口自动化测试?

An article to master the entire JVM, JVM ultra-detailed analysis!!!

最强大脑(1)

【静态代理】

tensorflow分词深度学习——影评预测
随机推荐
JavsSE => 多态
FPGA工程师面试试题集锦41~50
`id` bigint(20) unsigned NOT NULL COMMENT 'Database primary key',
oracle cdc时,设置并行度2插槽数1,最终任务只有一个tm,是不是因为oracle不支持并发
栈与队列 | 有效的括号、删除字符串中的所有相邻元素、逆波兰表达式求值、滑动窗口的最大值、前K个高频元素 | leecode刷题笔记
Linear Algebra (4)
栈与队列 | 用栈实现队列 | 用队列实现栈 | 基础理论与代码原理
FPGA engineer interview questions collection 11~20
什么是“大小端字节序”存储模式?
Guys, the test in the idea uses FlinkCDC SQL to read Mysql data and write it into Kafka. The code creates
SQLSERVER 2008 parses data in Json format
Big guys, mysql cdc (2.2.1 and previous versions) sometimes has this situation since savepoint, is there anything wrong?
安芯电子IPO过会:年营收4亿 汪良恩兄弟持股61.6%
2022 security officer C certificate test and simulation test in shandong province
一文带你搞懂OAuth2.0
如何模拟后台API调用场景,很细!
Get started with the OAuth protocol easily with a case
告诉你如何从keil工程知道使用了多少RAM和ROM空间
tensorflow分词深度学习——影评预测
mysql常用命令有什么