Mysql 中 json、JsonArray 类型字段中指定属性的模糊查询问题

解决json类型字段的模糊查询:

存储的数据格式:{"type": "10", "mobile": "13545678900", "countryCode": "86"}

select * from a where mobile_json->'$.mobile' like '%135%'

解决json类型字段的精确查询

数据存储格式:{"type": "10", "mobile": "13545678900", "countryCode": "86"}

select * from a where mobile_json-> '$.mobile' = 13545678900

解决 JsonArray 类型字段的模糊查询:

存储的数据格式: [{"type": "10", "mobile": "13545678900", "countryCode": "86"}]

select * from a where mobile_json->'$[*].mobile' like '%135%'

select * from a where JSON_EXTRACT(mobile_json, '\[\*\].mobile') LIKE '%135%'

解决 JsonArray 类型字段的精确查询:

存储的数据格式: [{"type": "10", "mobile": "13545678900", "countryCode": "86"}]

select * from a where JSON_CONTAINS(mobile_json,JSON_OBJECT('mobile', "13545678900"))

thinkphp 或 laravel 写法

复制代码
// 精确匹配
$query->whereRaw("JSON_CONTAINS(ext_data, JSON_OBJECT('jump_type', ". $value."))" ) 
复制代码
// 模糊匹配
$query->whereRaw("items_json->'$[*].name' like '%$value%'" );
复制代码
$query->whereRaw("JSON_EXTRACT(items_json, '$[*].name') LIKE '%$value%'" );

参考文献:

mysql查询json数组内字段模糊 - 老白网络

mysql查询json内字段 mysql查询json数组内字段模糊_恋上一只猪的技术博客_51CTO博客

学习笔记 如有侵权,请联系删除

相关推荐
程序员三明治2 小时前
【MyBatis从入门到入土】告别JDBC原始时代:零基础MyBatis极速上手指南
数据库·mysql·mybatis·jdbc·数据持久化·数据
知其然亦知其所以然2 小时前
面试官一开口就问:“你了解MySQL水平分区吗?”我当场差点懵了……
后端·mysql·面试
咖啡Beans2 小时前
MySQL的JSON_函数总结
mysql
你们瞎搞3 小时前
arcgis矢量数据转为标准geojson格式
python·arcgis·json·地理空间数据
神仙别闹3 小时前
基于Java(Spring Boot)+MySQL实现电商网站
java·spring boot·mysql
hacker_LeeFei4 小时前
Springboot连接多数据源(MySQL&Oracle)
spring boot·mysql·oracle
潇凝子潇4 小时前
MySQL Redo Log 和 Undo Log 满了会有什么问题
数据库·mysql
周杰伦_Jay6 小时前
【Homebrew安装 MySQL 】macOS 用 Homebrew 安装 MySQL 完整教程
数据库·mysql·macos
贝多芬也爱敲代码13 小时前
如何减小ES和mysql的同步时间差
大数据·mysql·elasticsearch
安当加密14 小时前
MySQL数据库透明加密(TDE)解决方案:基于国密SM4的合规与性能优化实践
数据库·mysql·性能优化