文章目录 1. 函数说明2. JSON对象3. 字符串JSON数组3.1 AND关系3.2 OR关系 4. 对象数组5. 集成 Mybatis plus6. 模糊查询7. j
file_type
可以是 varchar
,也可以是 JSON
类型 jsON_CONTaiNS(json_doc, val[, path])
:判断是否包含某个json值
JSON_ARRAY([val[, val] ...])
:创建json数组
{"key": 1, "name": "万飞"}
查询
SELECT * FROM `ak_file_config` where file_type -> '$.name' = "万飞"
["EXE", "白加黑", "DLL"]
查询
SELECT * FROM `ak_file_config` where JSON_CONTAINS(file_type, JSON_ARRAY("白加黑","DLL"))
SELECT * FROM `ak_file_config` where JSON_CONTAINS(file_type,'"DLL"') OR JSON_CONTAINS(file_type,'"EXE"')
// jsonArray查询.apply(CollUtil.isNotEmpty(query.getFileType()), StrUtil.format("JSON_CONTAINS(t.file_type, JSON_ARRAY({}))", // 设置占位符{0},{1},{2} IntStream.range(0, Optional.ofNullable(query.getFileType()).orElse(Collections.emptyList()).size()) .mapToObj(i -> "{".concat(String.valueOf(i)).concat("}")) .collect(Collectors.joining(","))), Optional.ofNullable(query.getFileType()).orElse(Collections.emptyList()).toArray())
参考 https://blog.csdn.net/qq_31832209/article/details/125374325
SELECT* FROM`ak_file_config` WHEREJSON_EXTRACT(file_type, '$' ) LIKE '%DL%';
代码里参考
String productOrCompanyName = query.getProductOrCompanyName().replace("\"", "_");.and(StrUtil.isNotBlank(productOrCompanyName), wq -> wq.apply("JSON_EXTRACT(LOWER(t.label_involve_product), '$') LIKE LOWER(CONCAT('%', {0}, '%'))", productOrCompanyName) .or() .apply("JSON_EXTRACT(LOWER(t.label_involve_company), '$') LIKE LOWER(CONCAT('%', {0}, '%'))", productOrCompanyName))
如果输入字符串带双引号,需要将
\"
替换成_
进行模糊搜索,但是会查询出不带双引号的数据
Mysql
最低版本8.0.4
SELECT* FROMJSON_TABLE ( '["11", "22"]', '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result INT PATH '$' ) ) ) AS t;
json
对象数组参考 https://cdn.modb.pro/db/484630
在表中关联字符串数组分组查询
需求:统计各类型的数量
SELECTt1.result,count( t1.result ) AS count FROMpe_main_body tINNER JOIN JSON_TABLE ( t.overview_product_type, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result VARCHAR ( 100 ) PATH '$' ) ) ) AS t1 WHEREdel_flag = FALSE GROUP BYt1.result
例如:match_context
字段是对象数组,对象里面有两个字段keyWord
和describe
需求:查询出所有的不重复的对象
SELECT DISTINCTt1.result FROMii_sensitive_resource_info tINNER JOIN JSON_TABLE ( t.match_context, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result JSON PATH '$' ) ) ) AS t1 WHEREcompany_id IN ( 296 )
DISTINCT
:去重
改为字段返回
SELECTresult ->> '$.keyWord' AS keyWord,result ->> '$.describe' AS descInfo FROM(SELECT DISTINCTt1.result FROMii_sensitive_resource_info tINNER JOIN JSON_TABLE ( t.match_context, '$[*]' COLUMNS ( NESTED PATH '$' COLUMNS ( result JSON PATH '$' ) ) ) AS t1 WHEREcompany_id IN ( 296 )) tmp
->>
:会去除双引号
JSONArray
符号是$[*]
,$[0]
是数组对象时,不能查询所有,老版本不支持$[*]
,使用新版本JSONObject
符号是$
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' LIKE "%b%"
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' = "bbb"
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE CAST(JSON_UNQUOTE(`address`->'$[0].date') AS DATETIME) BETWEEN '2023-08-13' AND '2023-08-17'
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE `address`->'$[0].name' IN ("bbb","ccc")
SELECT * FROM `tf_cloud`.`tf_low_data_testUser` WHERE json_contains(`address`->"$[*].nickname",'["bbb"]')
来源地址:https://blog.csdn.net/qq_38983728/article/details/126144608
--结束END--
本文标题: Mysql JSON对象和JSON数组查询
本文链接: https://lsjlt.com/news/395033.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0