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

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

相关推荐
weixin_511255215 小时前
MySQL升级记录
数据库·mysql
做运维的阿瑞6 小时前
数据库增删改的安全写法
数据库·sql·mysql
frjc8 小时前
数据库选型:如何从众多数据库中选出最理想的那一个
redis·mysql·clickhouse·elasticsearch
小马哥程序开发8 小时前
[点赞收藏免费领取 · 项目源码]37399民族服饰饰品商城小程序
sql·mysql·flask·源码·课程设计·程序开发·课设
数据库小学妹12 小时前
MySQL大表怎么优化?2亿行表的分区归档与冷热分离实战
数据库·mysql·分库分表·分区表·数据库运维·冷热分离
这个DBA有点耶13 小时前
连接池与MySQL交互实战:连接风暴、连接泄漏与连接状态异常排查
数据库·mysql·架构
需要82614 小时前
MySQL MVCC 与事务隔离级别:从一条 update 看版本链
java·数据库·spring boot·mysql·spring cloud
夜雪一千14 小时前
MySQL 默认值使用方法
数据库·mysql
bksczm14 小时前
MySQL进阶篇之范式及E-R图
数据库·sql·mysql
Shadow(⊙o⊙)15 小时前
表的增删查改
数据库·mysql