1、json对象
1.1、方法
使用对象操作的方法进行查询:字段->'$.json属性'
使用函数进行查询:json_extract(字段, '$.json属性')
获取JSON数组/对象长度:JSON_LENGTH()
1.2、数据
copy {
"tag" : [ "GRS" , "GOTS" ] ,
"size" : "M" ,
"color" : "红色" ,
"material" : "尼龙"
}
copy CREATE TABLE `test` (
`id` bigint (20 ) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID' ,
`goods_sn` varchar (25 ) NOT NULL DEFAULT '' COMMENT '商品编码' ,
`desc_attr` json NOT NULL COMMENT '描述属性' ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB COMMENT='TEST' ;
INSERT INTO `test`.`test`(`id`, `goods_sn`, `desc_attr`) VALUES (1 , 'A0001' , '{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"红色\", \"material\": \"尼龙\"}' );
INSERT INTO `test`.`test`(`id`, `goods_sn`, `desc_attr`) VALUES (2 , 'A0002' , '{\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"size\": \"LA\", \"color\": \"黄色\", \"material\": \"纯棉\"}' );
1.3、查询
copy
select * from test where desc_attr- > '$.material' is not null ;
select * from test where JSON_EXTRACT(desc_attr, '$.material' ) is not null ;
select * from test where desc_attr- > '$.material' = '纯棉' ;
select * from test where JSON_EXTRACT(desc_attr, '$.material' )= '纯棉' ;
select * from test where JSON_LENGTH(desc_attr- > '$.tag' )> 2 ;
2、json数组
2.1、方法
对象操作方式查询:字段->'$[0].属性'
使用函数查询:JSON_CONTAINS(字段,JSON_OBJECT('json属性', '内容'))
获取JSON数组/对象长度:JSON_LENGTH()
2.2、数据
copy [
{
"tag" : [ "GRS" , "GOTS" ] ,
"size" : "M" ,
"color" : "红色" ,
"material" : "尼龙"
} ,
{
"tag" : [ "GRS" , "GOTS" , "MTD" ] ,
"size" : "LA" ,
"color" : "黄色" ,
"material" : "纯棉"
}
]
copy CREATE TABLE `test2` (
`id` bigint (20 ) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID' ,
`goods_sn` varchar (25 ) NOT NULL DEFAULT '' COMMENT '商品编码' ,
`desc_attrs` json NOT NULL COMMENT '描述属性,多个' ,
PRIMARY KEY (`id`)
) ENGINE= InnoDB COMMENT= 'TEST2' ;
INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (1 , 'A0001' , '[{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"红色\", \"material\": \"尼龙\"}, {\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"size\": \"LA\", \"color\": \"黄色\", \"material\": \"纯棉\"}]' );
INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (2 , 'A0002' , '[{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"红色\", \"material\": \"尼龙\"}, {\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"link\": \"xxx\", \"size\": \"LA\", \"color\": \"黄色\", \"material\": \"纯棉\"}]' );
INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (3 , 'A0003' , '[]' );
2.3、查询
copy
select * from test2 where JSON_LENGTH(desc_attrs) > 0 ;
select * from test2 where desc_attrs- > '$[0].color' is not null ;
select * from test2 where desc_attrs- > '$[*].link' is not null ;
select * from test2 where JSON_CONTAINS(desc_attrs,JSON_OBJECT ('link' , 'xxx' ));
copy
select desc_attrs- > '$[*].link' from test2 where id= 2 ;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?