mysql根据JSON字段内容查询数据

原文:https://www.cnblogs.com/sensenh/p/15397864.html

mysql5.7以上支持json的操作,以及增加了json存储类型
一般数据库存储json类型的数据会用json类型或者text类型

表数据

表结构

 

使用 字段->’$.json属性’进行查询条件

select  * from log where data->'$.id' = 142;

 select data->'$.id' id,data->'$.name' name from log where data->'$.id' = 142;

 

 

 更多方案:https://www.jb51.net/article/230855.htm

。表数据

 

select * from product where suit like '%"10001"%';
#like方式不能使用索引,性能不佳,且准确性不足

select * from product where suit LOCATE('"10001"', 'suit') > 0;
# LOCATE方式和like存在相同问题

select * from product where suit != '' and json_contains('suit'->'$.hotel', '"10001"');
#以MySQL内置json函数查找,需要MySQL5.7以上版本才能支持,准确性较高,不能使用全文索引

select * from product where MATCH(suit) AGAINST('+"10001"' IN BOOLEAN MODE);
#可使用全文索引,MySQL关键字默认限制最少4个字符,可在mysql.ini中修改 ft_min_word_len=2,重启后生效

 

posted @ 2021-12-28 09:23  zagwk  阅读(2358)  评论(0编辑  收藏  举报