当前位置:网站首页>MySQL_关于JSON数据的查询
MySQL_关于JSON数据的查询
2022-07-31 05:07:00 【m0_67390379】
MySQL中关于JSON类型数据的查询
导语
MySQL从5.7版本开始支持JSON格式的数据类型,JSON格式的字符串类型有如下优势:
1、存储在JSON列中的JSON文档会被自动验证。
无效的文档会产生错误;
2、最佳存储格式。
存储在JSON列中的JSON文档会被转换为允许快速读取文档元素的内部格式。
JSON格式的数据
{
"classInfo": [
{
"level": 1,
"classId": 1472869278473637889,
"parentId": 0,
"typeGroups": 1472869277552140288
},
{
"level": 2,
"classId": 1472869580589355009,
"parentId": 1472869278473637889,
"typeGroups": 1472869580531884032
},
{
"level": 3,
"classId": 1472869739033382914,
"parentId": 1472869580589355009,
"typeGroups": 1472869738959134720
},
{
"level": 4,
"classId": 1472875592394231810,
"parentId": 1472869739033382914,
"typeGroups": 1472875591372201984
}
],
"classValueInfo": {
"specs": [],
"basics": [
{
"valueIds": [],
"basicsType": "model",
"typeGroups": []
},
{
"valueIds": [
1470582565747314690
],
"basicsType": "内部品牌",
"typeGroups": [
1470582565592760320
]
},
{
"valueIds": [],
"basicsType": "适用品牌",
"typeGroups": []
}
],
"skuSpecs": [
{
"id": null,
"price": 100,
"skuCode": "1473137918587781120",
"upcCode": "1",
"inventory": 10,
"skuSpecInfos": [],
"thirdpartySkuCode": "1"
}
],
"attributes": [
{
"type": "重要属性",
"value": "1",
"typeGroups": 1470588912652648448,
"attributeId": 1470588912693956611,
"valueIdList": []
},
{
"type": "重要属性",
"value": "",
"typeGroups": 1470589592058593280,
"attributeId": 1470589592070541314,
"valueIdList": [
{
"valueId": 1470603255703789569,
"valueIdTypeGroups": 1470602701724946432
}
]
}
]
}
}
表信息
CREATE TABLE `product` (
`id` bigint NOT NULL,
`site_code` varchar(16) COLLATE utf8mb4_general_ci NOT NULL COMMENT '站点代码',
`product_code` varchar(50) COLLATE utf8mb4_general_ci NOT NULL COMMENT 'SPU代码',
`product_features` longtext COLLATE utf8mb4_general_ci COMMENT 'SPU 商品特征',
`product_m_describe` longtext COLLATE utf8mb4_general_ci COMMENT 'SPU M端描述',
`product_pc_describe` longtext COLLATE utf8mb4_general_ci COMMENT 'SPU PC端描述',
`product_class` json NOT NULL COMMENT '冗余的商品分类信息',
`spec_type` tinyint NOT NULL COMMENT '规格类型 1=单规格 2=多规格',
`url` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '自定义Url',
`up_time` datetime DEFAULT NULL COMMENT '上架时间',
`down_time` datetime DEFAULT NULL COMMENT '下架时间',
`sort` int NOT NULL DEFAULT '0' COMMENT '排序',
`del_flag` tinyint NOT NULL DEFAULT '1' COMMENT '是否删除 0=删除,1=正常',
`create_by` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
`create_time` datetime NOT NULL,
`update_by` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
`update_time` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='商品分类'
表结构不是重点,终点是存在JSON数据类型的字段,将JSON格式数据粘贴到对应的字段中即可,上面的表结构中product_class为Json数据类型的字段
开始查询
SQL语句
SELECT * FROM product WHERE del_flag=1
AND (JSON_CONTAINS(product_class>'$.classInfo',JSON_OBJECT('classId',1470584780293689347))
AND JSON_CONTAINS(product_class->'$.classInfo',JSON_OBJECT('classId', 1470585059726610434))
AND JSON_CONTAINS(product_class->'$.classInfo',JSON_OBJECT('classId', 1470592051933470721)))
查询结果

个人理解-关于SQL
JSON_CONTAINS(product_class->'$.classInfo',JSON_OBJECT('classId', 1470584780293689347))
官方文档中的解释
JSON_CONTAINS():JSON 文档是否包含路径中的特定对象
->:评估路径后从 JSON 列返回值;相当于 JSON_EXTRACT()。
JSON_OBJECT():创建 JSON 对象
SQL中包含的product_class是对应JSON格式的字段名,$.classInfo对应的是JSON数据中需要查询的列表,classId具体数据中的值
如果classInfo中包含有列表,name在查询的数据中classInfo的写法就要改变,继续"."下去,
例如:
'$.classInfo.A'
这里需要一直"."到你需要查询的那一级
结语
MySQL中关于JSON数据类型查询的方法当然不至这一种,有兴趣的可以具体去官网查看,
官网链接:MySQL官网-JSON函数参考
以上是个人理解,如果有不对的地方,希望博友指出
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held
- Sql解析转换之JSqlParse完整介绍
- What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?
- Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
- PWN ROP
- [C language] Detailed explanation of operators
- unity2d game
- MySQL事务隔离级别详解
- STM32——DMA
- SQL语句中对时间字段进行区间查询
猜你喜欢

Unity框架设计系列:Unity 如何设计网络框架

STM32 - DMA

工作流编排引擎-Temporal

SQL row-column conversion

prompt.ml/15中<svg>标签使用解释

Industry-university-research application to build an open source talent ecosystem | 2022 Open Atom Global Open Source Summit Education Sub-Forum was successfully held

Simple read operation of EasyExcel

sql statement - how to query data in another table based on the data in one table

PWN ROP

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
随机推荐
益智类游戏关卡设计:逆推法--巧解益智类游戏关卡设计
Mysql应用安装后找不到my.ini文件
CentOS7 - yum install mysql
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
MySQL optimization: from ten seconds to three hundred milliseconds
Unity框架设计系列:Unity 如何设计网络框架
npm、nrm两种方式查看源和切换镜像
Mysql application cannot find my.ini file after installation
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
参考代码系列_1.各种语言的Hello World
DVWA靶场环境搭建
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
PCL calculates the point cloud coordinate maximum and its index
Unity resources management series: Unity framework how to resource management
mysql uses on duplicate key update to update data in batches
【云原生】DevOps(五):集成Harbor
The monitoring of Doris study notes
Unity URP渲染管线摄像机核心机制剖析
Temporal介绍