【MySQL】根据JSON字段的内容检索查询数据

查询语句

  • 使用 字段->'$.json属性' 进行查询条件
  • 使用 json_extract 函数查询,json_extract(字段, "$.json属性")
  • 根据json数组查询,用 JSON_CONTAINS(字段, JSON_OBJECT('json属性', "内容"))

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

注意:用JSON类型的话
1)JSON列存储的必须是JSON格式数据,否则会报错。
2)JSON数据类型是没有默认值的。

使用示例:
1 演示数据

查询语句

SELECT * FROM test_event_list  where `subdata` -> '$.page_url' = 'https//www.test.com/123456.html';

select  *  from test_event_list where JSON_CONTAINS(`subdata`, JSON_OBJECT('page_url', "https//www.test.com/123456.html"));

select  *  from test_event_list   where json_extract(subdata,'$.page_url') = 'https//www.test.com/123456.html';

提示: 此方法在大批量数据下查询 缓慢,慎用!

原文地址:https://www.cnblogs.com/richerdyoung/p/14271308.html