MySql-Json函数Json数组查找JSON_SEARCH()使用-查找Json数组的值-JSON_SEARCH查找数值类型使用

文章目录

  • 前言
  • 一、对字符串类型进行查找
  • 二、对数值类型进行查找


前言

JSON_SEARCH()使用


一、对字符串类型进行查找

  1. 示例json:
[
{"day": 16, "userNo": "bd5b1e9ed1bc40f78c669ee868590aad", "salesNum": 22, "searchNum": 2, "publishNum": 6, "salesAmount": 44.28, "orderUserNum": 5, "newOrderUserNum": 2},
{"day": 17, "userNo": "919cbf1e743f421aa3d048863f0a4009", "salesNum": 5, "searchNum": 0, "publishNum": 2, "salesAmount": 13.04, "orderUserNum": 3, "newOrderUserNum": 0},
{"day": 18, "userNo": "20734604f0ab41c3bab7a5752aa40ece", "salesNum": 6,  "searchNum": 1, "publishNum": 5, "salesAmount": 18.21, "orderUserNum": 4, "newOrderUserNum": 1},
{"day": 19, "userNo": "919cbf1e743f421aa3d048863f0a4009", "salesNum": 3, "searchNum": 0, "publishNum": 8, "salesAmount": 8.52, "orderUserNum": 4, "newOrderUserNum": 1}
]

查找包含919cbf1e743f421aa3d048863f0a4009用户的数据

示例sql:

SELECT * 
FROM sjfw.count_product_category_sales_month 
where JSON_SEARCH(data_json,'all','919cbf1e743f421aa3d048863f0a4009');

;

二、对数值类型进行查找

解决MySqlJson字段的json列表查找方法JSON_SEARCH()只能对json列表里面的字符串类型的数据进行查找不能对数值类型的数据进行查找问题

需要查询到day=18的数据

select JSON_EXTRACT(data_json, concat('$[',aa,']')),aa,data_json
from (
         SELECT cast(replace(replace(JSON_SEARCH(
                                             REPLACE(REPLACE(REPLACE(JSON_EXTRACT(data_json, "$[*].day"),', ', '","'), '[', '["'),']', '"]'),
                                             'all', '18') ,'"$[',''),']"','') as SIGNED) aa,data_json
         FROM sjfw.count_product_sku_sales_month
         where month='2022-08' and sku='CJBHNSNS25088'
     )t
;

你可能感兴趣的:(mysql,java,大数据,json,mysql,大数据)