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博客

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

相关推荐
攒了一袋星辰1 小时前
高并发强一致性顺序号生成系统 -- SequenceGenerator
java·数据库·mysql
顶点多余2 小时前
使用C/C++语言链接Mysql详解
数据库·c++·mysql
Seven974 小时前
MySQL语句执行深度剖析:从连接到执行的全过程
mysql
总要冲动一次5 小时前
离线安装 percona-xtrabackup-24
linux·数据库·mysql·centos
buhuimaren_6 小时前
MySQL数据库初体验
数据库·mysql
J超会运6 小时前
MySQL核心SQL语句速查宝典
数据库·mysql
殷紫川6 小时前
吃透分库分表:分片策略、跨库事务与平滑扩容全解
mysql·架构
殷紫川7 小时前
SQL 性能优化全解:从执行计划到底层逻辑,根治 99% 的慢 SQL 与规范落地
数据库·mysql
殷紫川7 小时前
MySQL高可用生产落地全解:主从同步、MGR集群、读写分离从原理到实战
mysql·架构
ego.iblacat7 小时前
MySQL 数据库操作
数据库·mysql·adb